deno.land / x / masx200_leetcode_test@10.6.5 / add-binary / index.ts

نووسراو ببینە
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
export default function addBinary(num1: string, num2: string): string { const result: number[] = []; let carry = 0; for (let i = 0; i < Math.max(num2.length, num1.length); i++) { // console.log(i, num1[i], num2[i], carry); const r = carry + Number(getIntegerReverse(num1, i)) + Number(getIntegerReverse(num2, i)); result.push(r % 2); carry = Math.floor(r / 2); } if (carry) result.push(carry); return result.reverse().join("");}function getIntegerReverse(s: string, i: number) { if (i < 0) { throw Error("out of bounds"); } if (i < s.length) { return Number(s.slice(-1 - i)[0]); } else { return 0; }}
masx200_leetcode_test

Version Info

Tagged at
a year ago