Skip to content

Commit 82b2ab7

Browse files
committed
Solution of 159 and 170 problems
1 parent d79fd2e commit 82b2ab7

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

100-200q/159.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Given a string, find the longest substring that contains only two unique characters. For example, given "abcbbbbcccbdddadacb", the longest substring that contains 2 unique character is "bcbbbbcccb".
3+
'''
4+
5+
class Solution(object):
6+
def lengthOfLongestSubstringTwoDistinct(self, s):
7+
"""
8+
:type s: str
9+
:rtype: int
10+
"""
11+
if not s:
12+
return 0
13+
14+
unique_char, start, result = {}, 0, 0
15+
for index, char in enumerate(s):
16+
if char in unique_char:
17+
unique_char[s] += 1
18+
else:
19+
unique_char[s] = 1
20+
21+
if len(unique_char) <= 2:
22+
result = max(result, index-start+1)
23+
else:
24+
while len(unique_char) > 2:
25+
char_index = s[start]
26+
count = unique_char[char_index]
27+
if count == 1:
28+
del unique_char[char_index]
29+
else:
30+
unique_char[char_index] -= 1
31+
start += 1
32+
return result

100-200q/170.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
Design and implement a TwoSum class. It should support the following operations: add and find.
3+
4+
add – Add the number to an internal data structure.
5+
find – Find if there exists any pair of numbers which sum is equal to the value.
6+
7+
For example,
8+
add(1); add(3); add(5);
9+
find(4) -> true
10+
find(7) -> false
11+
'''
12+
13+
class TwoSum(object):
14+
15+
def __init__(self):
16+
"""
17+
initialize your data structure here
18+
"""
19+
self.value_count = {}
20+
21+
def add(self, number):
22+
"""
23+
Add the number to an internal data structure.
24+
:rtype: nothing
25+
"""
26+
if number in self.value_count:
27+
self.value_count[number] += 1
28+
else:
29+
self.value_count[number] = 1
30+
31+
def find(self, value):
32+
"""
33+
Find if there exists any pair of numbers which sum is equal to the value.
34+
:type value: int
35+
:rtype: bool
36+
"""
37+
for val in self.value_count:
38+
diff = value - val
39+
if diff in self.value_count and (diff != val or self.value_count[val] > 1):
40+
return True
41+
return False
42+
43+
# Your TwoSum object will be instantiated and called as such:
44+
# twoSum = TwoSum()
45+
# twoSum.add(number)
46+
# twoSum.find(value)

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
118118
|191|[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/)| [Python](./100-200q/191.py)|Easy|
119119
|179|[Largest Number](https://leetcode.com/problems/largest-number/) | [Python](./100-200q/179.py)|Medium|
120120
|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator)|[Python](./100-200q/173.py)|Medium|
121+
|170|[Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design)|[Python](./100-200q/170.py)|Easy|
121122
|162|[Find Peak Element](https://leetcode.com/problems/find-peak-element/) | [Python](./100-200q/162.py)|Medium|
122123
|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [Python](./100-200q/160.py)|Easy|
124+
|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|
123125
|155|[Min Stack](https://leetcode.com/problems/min-stack/)| [Python](./100-200q/155.py)|Easy|
124126
|152|[Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)| [Python](./100-200q/152.py)|Medium|
125127
|150|[Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/)|[Python](./100-200q/150.py)|Medium|

0 commit comments

Comments
 (0)