Skip to content

Commit 69739a9

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 371
1 parent e514ee6 commit 69739a9

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
- [338 Counting Bits](https://leetcode.com/problems/counting-bits/description/)
230230
- [342 Power of Four](https://leetcode.com/problems/power-of-four/description/)
231231
- [354 Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes/description/)
232+
- [371 Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/description/)
232233
- [373 Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/)
233234
- [377 Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/description/)
234235
- [380 Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/description/)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def getSum(self, a: int, b: int) -> int:
5+
"""
6+
Given two integers a and b, return the sum of the two integers without using
7+
the operators + and -.
8+
"""
9+
MASK = 0xFFFFFFFF # Mask to get last 32 bits
10+
MAX_INT = 0x7FFFFFFF # Max positive 32-bit integer
11+
12+
a &= MASK
13+
b &= MASK
14+
15+
while b != 0:
16+
carry = (a & b) << 1
17+
a = a ^ b
18+
b = carry & MASK
19+
20+
return a if a <= MAX_INT else ~(a ^ MASK)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._371_sum_of_two_integers import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["a", "b", "expected"],
8+
argvalues=[
9+
(1, 2, 3),
10+
(2, 3, 5),
11+
],
12+
)
13+
def test_func(a: int, b: int, expected: int):
14+
"""Tests the solution of a LeetCode problem."""
15+
sum = Solution().getSum(a, b)
16+
assert sum == expected

0 commit comments

Comments
 (0)