Skip to content

Commit 487873d

Browse files
committed
feat: sum-of-two-integers solution
1 parent fc5e450 commit 487873d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

sum-of-two-integers/YeomChaeeun.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* 연산자 + 사용하지 않고 덧셈하기
3+
* 알고리즘 복잡도
4+
* - 시간 복잡도: O(logn) - 비트 수만큼 계산
5+
* - 공간 복잡도: O(1)
6+
* @param a
7+
* @param b
8+
*/
9+
function getSum(a: number, b: number): number {
10+
while(b !== 0) {
11+
let carry = a & b; // and - 11 & 10 = 10
12+
a = a ^ b; // xor - 11 ^ 10 = 01
13+
b = carry << 1; // 100
14+
}
15+
16+
return a
17+
}

0 commit comments

Comments
 (0)