문제
문제 링크 : Add Binary
풀이
/**
* @param {string} a
* @param {string} b
* @return {string}
*/
var addBinary = function(a, b) {
let binA = BigInt("0b"+a)
let binB = BigInt("0b"+b)
return (binA+binB).toString(2);
};
- a,b를 10진수의 BigInt값으로 바꾸고
- 두 값을 더한 후 2진수로 변환한 값을 return
- Runtime 62 ms, Memory 42.2 MB
다른 풀이
/**
* @param {string} a
* @param {string} b
* @return {string}
*/
var addBinary = function(a, b) {
return ((BigInt(`0b${a}`)+BigInt(`0b${b}`)).toString(2));
};
- 기존 풀이를 한 줄로 줄인 풀이
- Runtime 51 ms, Memory 41.8 MB
'알고리즘 풀이 > leetcode' 카테고리의 다른 글
[leetcode, JS] 66. Plus One (0) | 2023.09.26 |
---|---|
[leetcode, JS] 58. Length of Last Word (0) | 2023.09.25 |
[leetcode, JS] 28. Find the Index of the First Occurrence in a String (0) | 2023.09.25 |
[leetcode, JS] 27. Remove Element (0) | 2023.09.25 |
[leetcode, JS] 26. Remove Duplicates from Sorted Array (0) | 2023.09.22 |