371. Sum of Two Integers
Tags:
Medium
Skills:
Bitwise
June 24, 2025
04:32 AM
No headings found
Loading content...
Related Posts
Leetcode
No headings found
Related Posts
Leetcode
Problem
nhớ (carry)nhớ (carry) cần cộng vào ở lượt tiếp theoMinh họa
Ví dụ:
a = 2 (10), b = 3 (11)
=> Kết quả là 5.
Solution
1function getSum(a: number, b: number): number {
2 while (b !== 0) {
3 const output = a ^ b;
4 const carry = (a & b) << 1;
5 a = output;
6 b = carry;
7 }
8 return a
9};