Skip to content

Commit 48fc33d

Browse files
committed
Solution of UTF-8 validation and Misssing Ranges
1 parent b3ee24c commit 48fc33d

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

100-200q/163.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], return its missing ranges.
3+
4+
For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76->99"].
5+
'''
6+
7+
class Solution(object):
8+
def missingRange(self, A, lower, upper):
9+
if not A:
10+
return []
11+
12+
result = []
13+
if A[0] != lower:
14+
end = A[0] - 1
15+
if end == lower:
16+
m_r = str(lower)
17+
else:
18+
m_r = str(lower) + "->" + str(end)
19+
result.append(m_r)
20+
21+
for index in range(1, len(A)):
22+
if A[index] != A[index-1] + 1:
23+
start = A[index-1] + 1
24+
end = A[index] - 1
25+
if start == end:
26+
m_r = str(start)
27+
else:
28+
m_r = str(start) + "->" + str(end)
29+
result.append(m_r)
30+
31+
if A[len(A) - 1] != upper:
32+
start = A[len(A)-1] + 1
33+
if start == upper:
34+
m_r = str(start)
35+
else:
36+
m_r = str(start) + "->" + str(upper)
37+
result.append(m_r)
38+
return result
39+
40+
solution = Solution()
41+
print solution.missingRange([0, 1, 3, 50, 75], 0, 99)
42+
print solution.missingRange([4, 10, 50, 98], 0, 99)
43+
print solution.missingRange([0], 0, 1)

300-400q/393.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'''
2+
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
3+
4+
For 1-byte character, the first bit is a 0, followed by its unicode code.
5+
For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.
6+
This is how the UTF-8 encoding would work:
7+
8+
Char. number range | UTF-8 octet sequence
9+
(hexadecimal) | (binary)
10+
--------------------+---------------------------------------------
11+
0000 0000-0000 007F | 0xxxxxxx
12+
0000 0080-0000 07FF | 110xxxxx 10xxxxxx
13+
0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
14+
0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
15+
Given an array of integers representing the data, return whether it is a valid utf-8 encoding.
16+
17+
Note:
18+
The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data.
19+
20+
Example 1:
21+
22+
data = [197, 130, 1], which represents the octet sequence: 11000101 10000010 00000001.
23+
24+
Return true.
25+
It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character.
26+
Example 2:
27+
28+
data = [235, 140, 4], which represented the octet sequence: 11101011 10001100 00000100.
29+
30+
Return false.
31+
The first 3 bits are all one's and the 4th bit is 0 means it is a 3-bytes character.
32+
The next byte is a continuation byte which starts with 10 and that's correct.
33+
But the second continuation byte does not start with 10, so it is invalid.
34+
'''
35+
36+
class Solution(object):
37+
def validUtf8(self, data):
38+
"""
39+
:type data: List[int]
40+
:rtype: bool
41+
"""
42+
seveneth_mask = 1 << 7
43+
sixth_mask = 1 << 6
44+
no_bytes = 0
45+
46+
if len(data) == 1:
47+
return not(data[0] & seveneth_mask)
48+
49+
for num in data:
50+
if no_bytes == 0:
51+
mask = 1 << 7
52+
53+
while num & mask:
54+
no_bytes += 1
55+
mask >>= 1
56+
57+
if no_bytes == 0:
58+
continue
59+
60+
if no_bytes == 1 or no_bytes > 4:
61+
return False
62+
else:
63+
if not(num & seveneth_mask and not(num & sixth_mask)):
64+
return False
65+
no_bytes -= 1
66+
return no_bytes == 0

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
104104
##### [Problems 300-400](./300-400q/)
105105
| # | Title | Solution | Difficulty |
106106
|---| ----- | -------- | ---------- |
107+
|393|[UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/)|[Python](./300-400q/393.py)|Medium|
107108
|388|[Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path)|[Python](./300-400q/388.py)|Medium|
108109
|387|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [Python](./300-400q/387.py)|Easy|
109110
|380|[Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/) | [Python](./300-400q/380.py)|Hard|
@@ -164,6 +165,7 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
164165
|179|[Largest Number](https://leetcode.com/problems/largest-number/) | [Python](./100-200q/179.py)|Medium|
165166
|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator)|[Python](./100-200q/173.py)|Medium|
166167
|170|[Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design)|[Python](./100-200q/170.py)|Easy|
168+
|163|[Missing Ranges](https://leetcode.com/problems/missing-ranges)[Python](./100-200q/163.py)|Medium|
167169
|162|[Find Peak Element](https://leetcode.com/problems/find-peak-element/) | [Python](./100-200q/162.py)|Medium|
168170
|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [Python](./100-200q/160.py)|Easy|
169171
|159|[Longest Substring Which Contains 2 Unique Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters)|[Python](./100-200q/159.py)|Hard|

0 commit comments

Comments
 (0)