Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3e2a3fd

Browse files
D. J.:
- Added the leetcode problem and solution for 476
1 parent 2c9ace2 commit 3e2a3fd

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
- [452 Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
247247
- [461 Hamming Distance](https://leetcode.com/problems/hamming-distance/description/)
248248
- [474 Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/description/)
249+
- [476 Number Complement](https://leetcode.com/problems/number-complement/description/)
249250
- [477 Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/description/)
250251
- [502 IPO](https://leetcode.com/problems/ipo/description/)
251252
- [509 Fibonacci Number](https://leetcode.com/problems/fibonacci-number/description/)
@@ -271,6 +272,7 @@
271272
- [931 Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/description/)
272273
- [933 Number of Recent Cells](https://leetcode.com/problems/number-of-recent-calls/description/)
273274
- [983 Minimum Cost for Tickets](https://leetcode.com/problems/minimum-cost-for-tickets/description/)
275+
- [1009 Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/description/)
274276
- [1027 Longest Arithmetic Subsequence](https://leetcode.com/problems/longest-arithmetic-subsequence/description/)
275277
- [1035 Uncrossed Lines](https://leetcode.com/problems/uncrossed-lines/description/)
276278
- [1137 N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/description/)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def bitwiseComplement(self, n: int) -> int:
5+
"""
6+
The complement of an integer is the integer you get when you flip all the 0's to
7+
1's and all the 1's to 0's in its binary representation.
8+
9+
For example, The integer 5 is "101" in binary and its complement is "010" which
10+
is the integer 2.
11+
12+
Given an integer n, return its complement.
13+
"""
14+
if n == 0:
15+
return 1
16+
17+
res = 0
18+
i = 0
19+
while n != 0:
20+
bit = n & 1
21+
res += (bit ^ 1) << i
22+
n >>= 1
23+
i += 1
24+
return res
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def findComplement(self, num: int) -> int:
5+
"""
6+
The complement of an integer is the integer you get when you flip all the 0's
7+
to 1's and all the 1's to 0's in its binary representation.
8+
- For example, The integer 5 is "101" in binary and its complement is "010"
9+
which is the integer 2.
10+
11+
Given an integer num, return its complement.
12+
"""
13+
if num == 0:
14+
return 1
15+
16+
res = 0
17+
i = 0
18+
while num != 0:
19+
bit = num & 1
20+
res += (bit ^ 1) << i
21+
num >>= 1
22+
i += 1
23+
return res
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._1009_complement_of_base_10_integer import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["num", "expected"],
8+
argvalues=[
9+
(5, 2),
10+
(7, 0),
11+
(10, 5),
12+
(0, 1),
13+
],
14+
)
15+
def test_func(num: int, expected: int):
16+
"""Tests the solution of a LeetCode problem."""
17+
complement = Solution().bitwiseComplement(num)
18+
assert complement == expected

‎tests/test_476_number_complements.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._476_number_complement import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["num", "expected"],
8+
argvalues=[
9+
(5, 2),
10+
(1, 0),
11+
(0, 1),
12+
],
13+
)
14+
def test_func(num: int, expected: int):
15+
"""Tests the solution of a LeetCode problem."""
16+
complement = Solution().findComplement(num)
17+
assert complement == expected

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /