Skip to content

Commit e0d8d73

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 17.8 MB (48.36%)
1 parent 007b673 commit e0d8d73

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<p>Given two integers <code>a</code> and <code>b</code>, return <em>the sum of the two integers without using the operators</em> <code>+</code> <em>and</em> <code>-</code>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
<pre><strong>Input:</strong> a = 1, b = 2
6+
<strong>Output:</strong> 3
7+
</pre><p><strong class="example">Example 2:</strong></p>
8+
<pre><strong>Input:</strong> a = 2, b = 3
9+
<strong>Output:</strong> 5
10+
</pre>
11+
<p>&nbsp;</p>
12+
<p><strong>Constraints:</strong></p>
13+
14+
<ul>
15+
<li><code>-1000 &lt;= a, b &lt;= 1000</code></li>
16+
</ul>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def getSum(self, a: int, b: int) -> int:
3+
a, b = a & 0xFFFFFFFF, b & 0xFFFFFFFF
4+
while b:
5+
carry = ((a & b) << 1) & 0xFFFFFFFF
6+
a, b = a ^ b, carry
7+
8+
return a if a < 0x80000000 else ~(a ^ 0xFFFFFFFF)

0 commit comments

Comments
 (0)