Skip to content

Commit 77a9626

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 89 and 717
1 parent 815ac00 commit 77a9626

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
- [82 Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/)
134134
- [86 Partition List](https://leetcode.com/problems/partition-list/description/)
135135
- [88 Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/)
136+
- [89 Gray Code](https://leetcode.com/problems/gray-code/description/)
136137
- [91 Decode Ways](https://leetcode.com/problems/decode-ways/description/)
137138
- [92 Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/description/)
138139
- [95 Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/description/)
@@ -254,6 +255,7 @@
254255
- [673 Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/)
255256
- [712 Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/description/)
256257
- [714 Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/)
258+
- [717 1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/description/)
257259
- [724 Find Pivot Index](https://leetcode.com/problems/find-pivot-index/description/)
258260
- [735 Asteroid Collision](https://leetcode.com/problems/asteroid-collision/description/)
259261
- [740 Delete and Earn](https://leetcode.com/problems/delete-and-earn/description/)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def isOneBitCharacter(self, bits: List[int]) -> bool:
8+
"""
9+
We have two special characters:
10+
- The first character can be represented by one bit 0.
11+
- The second character can be represented by two bits (10 or 11).
12+
13+
Given a binary array bits that ends with 0, return true if the last character
14+
must be a one-bit character.
15+
"""
16+
i = 0
17+
while i < len(bits):
18+
if i == len(bits) - 2 and bits[i] == 1:
19+
return False
20+
if bits[i] == 1:
21+
i += 1
22+
i += 1
23+
return True
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def grayCode(self, n: int) -> List[int]:
8+
"""
9+
An n-bit gray code sequence is a sequence of 2n integers where:
10+
- Every integer is in the inclusive range [0, 2n - 1],
11+
- The first integer is 0,
12+
- An integer appears no more than once in the sequence,
13+
- The binary representation of every pair of adjacent integers differs by
14+
exactly one bit, and
15+
- The binary representation of the first and last integers differs by exactly
16+
one bit.
17+
18+
Given an integer n, return any valid n-bit gray code sequence.
19+
"""
20+
result = []
21+
total_numbers = 1 << n
22+
23+
for i in range(total_numbers):
24+
result.append(i ^ (i >> 1))
25+
26+
return result
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._717_1_bit_and_2_bit_characters import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["bits", "expected"],
10+
argvalues=[
11+
([1, 0, 0], True),
12+
([1, 1, 1, 0], False),
13+
],
14+
)
15+
def test_func(bits: List[int], expected: bool):
16+
"""Tests the solution of a LeetCode problem."""
17+
is_one_bit_character = Solution().isOneBitCharacter(bits)
18+
assert is_one_bit_character is expected

tests/test_89_gray_code.py

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

0 commit comments

Comments
 (0)