Skip to content

Commit 177c4a4

Browse files
committed
add sum of two integers solution
1 parent c5189fd commit 177c4a4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/** +, - ๋ถ€ํ˜ธ ์“ฐ์ง€ ์•Š๊ณ  ๋‘ ์ˆ˜์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ */
2+
class Solution {
3+
4+
// ๋น„ํŠธ ์—ฐ์‚ฐ์œผ๋กœ ๊ณ„์‚ฐ ์ˆ˜ํ–‰
5+
public int getSum(int a, int b) {
6+
while (b != 0) {
7+
// ์™ผ์ชฝ ์‰ฌํ”„ํŠธํ•˜์—ฌ ์ž๋ฆฌ ์˜ฌ๋ฆผ์— ๋Œ€ํ•œ ๊ณ„์‚ฐ ์ˆ˜ํ–‰
8+
int bit = (a & b) << 1;
9+
// XOR ์—ฐ์‚ฐ์œผ๋กœ ์ž๋ฆฌ ์˜ฌ๋ฆผ ์—†๋Š” ๋ง์…ˆ ์ˆ˜ํ–‰
10+
a = a ^ b;
11+
b = bit;
12+
}
13+
return a;
14+
}
15+
16+
/**
17+
* ํ’€๋ฆฌ๊ธฐ๋Š” ํ•˜์ง€๋งŒ, ์ด๋Ÿฐ ์‹์€ ํŽธ๋ฒ•์ผ ๊ฒƒ ๊ฐ™์€...?
18+
* Class Integer ๋‚ด๋ถ€์— sum ์—ฐ์‚ฐ: Adds two integers together as per the + operator.
19+
* + operator ์“ด๋‹ค๊ณ  ๋˜์–ด์žˆ์Œ...
20+
* */
21+
// public int getSum(int a, int b) {
22+
// return Integer.sum(a, b);
23+
// }
24+
25+
}
26+

0 commit comments

Comments
ย (0)