Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- html
- Recoil
- npm package
- camera access
- ffi-napi
- silent printing
- electron-packager
- vercel git lfs
- ELECTRON
- adb pair
- rolldown
- adb connect
- 이미지 데이터 타입
- device in use
- Git
- augmentedDevice
- animation
- github 100mb
- github lfs
- Each child in a list should have a unique "key" prop.
- dvh
- 티스토리 성능
- Failed to compiled
- Can't resolve
- camera permission
- github pdf
- react-native-dotenv
- react-native
- nextjs
- custom printing
Archives
- Today
- Total
Bleeding edge
[LeetCode] 67. Add Binary - 자바스크립트 0616 본문
https://leetcode.com/problems/add-binary/
Add Binary - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
이 문제를 풀기 위해서는 우선 2진수로 만드는 방법을 알아야 한다.
일반적으로 문제를 풀이 할 때는
let b = parseInt(a, '2')
let c = b.toString('2')
parseInt로 2진수의 숫자를 10진수로 바꾸고, toString으로 10진수를 2진수로 바꾸는 방법을 활용한다. 하지만 이 문제에서는 아주 큰 숫자를 다루기에 부동소수점 이슈로 인하여 문제가 큰 숫자에서는 오류가나온다. 그래서 어떤 문제를 사용할까 고민을 하다가, 두숫자를 reverse하여 뒤집고 그다음에 더하고 2가나오면 1이 나올때까지 계속 더한 뒤 다시 reverse를 할까 하다가 Big int와 binary를 최대한 활용하려고 검색을 해봤다.
"0b" + num을 활용하면, BigInt("0b" + num) 2진수를 BigInt로 바꿈과 동시에 10진수화 시킬수 있다!. 따라서
var addBinary = function (a, b) {
return (BigInt("0b" + a) + BigInt("0b" + b)).toString('2')
};
이렇게 풀이를 할 수 있다.