File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Constraints:
3
+ - -1000 <= a, b <= 1000
4
+
5
+ Time Complexity: O(1)
6
+
7
+ Space Complexity: O(1)
8
+ - ์ถ๊ฐ ๊ณต๊ฐ์ ์ฌ์ฉํ์ง ์๊ณ ์
๋ ฅ๋ฐ์ ๋ณ์๋ง ์ฌ์ฉ
9
+
10
+ ํ์ด๋ฐฉ๋ฒ:
11
+ 1. XOR(^)์ฐ์ฐ์ ํตํด ์บ๋ฆฌ๋ฅผ ์ ์ธํ ๊ฐ ์๋ฆฌ์ ํฉ์ ๊ตฌํจ
12
+ 2. AND(&)์ฐ์ฐ ํ ์ผ์ชฝ ์ํํธ(<<)๋ก ๋ค์ ์๋ฆฌ๋ก ์ฌ๋ผ๊ฐ ์บ๋ฆฌ๋ฅผ ๊ตฌํจ
13
+ 3. ์บ๋ฆฌ๊ฐ 0์ด ๋ ๋๊น์ง 1-2 ๊ณผ์ ์ ๋ฐ๋ณต
14
+ """
15
+ # Solution 1: ์ดํดํ๊ธฐ ์ฌ์ด ๋ฒ์
16
+ class Solution :
17
+ def getSum (self , a : int , b : int ) -> int :
18
+ while b :
19
+ current_sum = a ^ b
20
+
21
+ next_carry = (a & b ) << 1
22
+
23
+ a = current_sum
24
+ b = next_carry
25
+
26
+ return a
27
+
28
+ # Solution 2: ์ต์ ํ ๋ฒ์
29
+ class Solution :
30
+ def getSum (self , a : int , b : int ) -> int :
31
+ while b :
32
+ a , b = a ^ b , (a & b ) << 1
33
+
34
+ return a
35
+
You canโt perform that action at this time.
0 commit comments