Skip to content

Commit 1be32de

Browse files
committed
717_1-bit_and_2-bit_Characters
1 parent a180654 commit 1be32de

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal
185185
| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/706_Design_HashMap.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/706_Design_HashMap.java) | Hash implementation, mod is fine. Be careful about key conflict and key remove. |
186186
| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/709_To_Lower_Case.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/709_To_Lower_Case.java) | String processing:<br>1. str.lower() or str.toLowerCase()<br>2. ASCII processing. O(n) and O(1) |
187187
| 716 | [Max Stack](https://leetcode.com/problems/max-stack/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/716_Max_Stack.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/716_Max_Stack.java) | 1. Two stacks<br> 2. Double linked list and Hash |
188+
| 717 | [1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/717_1-bit_and_2-bit_Characters.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/717_1-bit_and_2-bit_Characters.java) | 1. Go through bits, 1 skip next, O(n) and O(1)<br>2. Find second last zero reversely, O(n) and O(1) |
188189
| 720 | [Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/720_Longest_Word_in_Dictionary.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/720_Longest_Word_in_Dictionary.java) | 1. Brute Force, O(sum(w^2)) and O(w)<br>2. Tire Tree, O(sum(w) and O(w))<br>3. Sort and word without last char, O(nlogn + sum(w)) and O(w) |
189190
| 724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/724_Find_Pivot_Index.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/724_Find_Pivot_Index.java) | Seach the array to find a place where left sum is equal to right sum, O(n) and O(1) |
190191
| 733 | [Flood Fill](https://leetcode.com/problems/flood-fill/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/733_Flood_Fill.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/733_Flood_Fill.java) | 1. DFS with stack or recursive, O(n) and O(n)<br>2. BFS with queue, O(n) and O(n) |
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11).
3+
4+
Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero.
5+
6+
Example 1:
7+
Input:
8+
bits = [1, 0, 0]
9+
Output: True
10+
Explanation:
11+
The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.
12+
Example 2:
13+
Input:
14+
bits = [1, 1, 1, 0]
15+
Output: False
16+
Explanation:
17+
The only way to decode it is two-bit character and two-bit character. So the last character is NOT one-bit character.
18+
Note:
19+
20+
1 <= len(bits) <= 1000.
21+
bits[i] is always 0 or 1.
22+
*/
23+
24+
// https://leetcode.com/problems/1-bit-and-2-bit-characters/solution/
25+
class Solution {
26+
public boolean isOneBitCharacter(int[] bits) {
27+
int pos = 0;
28+
// Go through bits
29+
while (pos < bits.length - 1) {
30+
// if 1, pos + 2; if 0, pos + 1
31+
pos += bits[pos] + 1;
32+
}
33+
return pos == bits.length - 1;
34+
}
35+
36+
/* public boolean isOneBitCharacter(int[] bits) {
37+
// From len - 2
38+
int i = bits.length - 2;
39+
// until encounter 0
40+
while (i >= 0 && bits[i] > 0) i--;
41+
// check if second last zero is even
42+
return (bits.length - i) % 2 == 0;
43+
} */
44+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11).
2+
# Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero.
3+
4+
# Example 1:
5+
# Input:
6+
# bits = [1, 0, 0]
7+
# Output: True
8+
# Explanation:
9+
# The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.
10+
# Example 2:
11+
# Input:
12+
# bits = [1, 1, 1, 0]
13+
# Output: False
14+
# Explanation:
15+
# The only way to decode it is two-bit character and two-bit character. So the last character is NOT one-bit character.
16+
# Note:
17+
18+
# 1 <= len(bits) <= 1000.
19+
# bits[i] is always 0 or 1.
20+
21+
# https://leetcode.com/problems/1-bit-and-2-bit-characters/solution/
22+
class Solution:
23+
def isOneBitCharacter(self, bits: List[int]) -> bool:
24+
pos = 0
25+
# Go through bits
26+
while pos < len(bits) - 1:
27+
# if 1, pos + 2; if 0, pos + 1
28+
pos += bits[pos] + 1
29+
return pos == len(bits) - 1
30+
31+
# def isOneBitCharacter(self, bits):
32+
# # From len - 2
33+
# pos = len(bits) - 2
34+
# # until encounter 0
35+
# while pos >= 0 and bits[pos] > 0:
36+
# pos -= 1
37+
# # check if second last zero is even
38+
# return (len(bits) - pos) % 2 == 0

0 commit comments

Comments
 (0)