Skip to content

Commit 75721fc

Browse files
committed
week 2
0 parents  commit 75721fc

File tree

15 files changed

+523
-0
lines changed

15 files changed

+523
-0
lines changed

1.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def twoSum(self, nums, target):
7+
"""
8+
:type nums: List[int]
9+
:type target: int
10+
:rtype: List[int]
11+
"""
12+
nums_dict = {}
13+
for index1, number1 in enumerate(nums):
14+
number2 = target - number1
15+
if number2 in nums_dict:
16+
return nums_dict[number2] + 1, index1 + 1
17+
nums_dict[number1] = index1

11.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def maxArea(self, height):
7+
"""
8+
:type height: List[int]
9+
:rtype: int
10+
"""
11+
length = len(height)
12+
left = 0
13+
right = length - 1
14+
max_area = 0
15+
while left < right:
16+
area = (right - left) * min(height[left], height[right])
17+
max_area = max(max_area, area)
18+
19+
if height[left] < height[right]:
20+
min_height = height[left]
21+
left += 1
22+
while height[left] < min_height:
23+
left += 1
24+
25+
else:
26+
min_height = height[right]
27+
right -= 1
28+
while height[right] < min_height:
29+
right -= 1
30+
31+
return max_area

12.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def intToRoman(self, num):
7+
"""
8+
:type num: int
9+
:rtype: str
10+
"""
11+
integer_symbols = [["I", "IV", "V", "IX"],
12+
["X", "XL", "L", "XC"],
13+
["C", "CD", "D", "CM"],
14+
["M"]
15+
]
16+
roman_str = ""
17+
counter = 0
18+
19+
while num != 0:
20+
single = num % 10
21+
22+
if single in [1, 2, 3]:
23+
roman_str = single * integer_symbols[counter][0] + roman_str
24+
elif single == 4:
25+
roman_str = integer_symbols[counter][1] + roman_str
26+
elif single == 5:
27+
roman_str = integer_symbols[counter][2] + roman_str
28+
elif single in [6, 7, 8]:
29+
roman_str = integer_symbols[counter][2] +\
30+
(single - 5) * integer_symbols[counter][0] +\
31+
roman_str
32+
elif single == 9:
33+
roman_str = integer_symbols[counter][3] + roman_str
34+
else:
35+
num = num / 10
36+
counter += 1
37+
continue
38+
num = num / 10
39+
counter += 1
40+
41+
return roman_str

13.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def romanToInt(self, s):
7+
"""
8+
:type s: str
9+
:rtype: int
10+
"""
11+
symbols_integer = {"I": 1, "V": 5, "X": 10, "L": 50,
12+
"C": 100, "D": 500, "M": 1000,
13+
"IV": 4, "IX": 9, "XL": 40, "XC": 90,
14+
"CD": 400, "CM": 900
15+
}
16+
length = len(s)
17+
integer = 0
18+
isPass = False
19+
for i in range(length):
20+
# Subtractive notation use this symbol
21+
if isPass:
22+
isPass = False
23+
continue
24+
# Just add the integer
25+
if s[i] in symbols_integer and s[i:i + 2] not in symbols_integer:
26+
integer = integer + symbols_integer[s[i]]
27+
isPass = False
28+
continue
29+
30+
# Subtractive notation is used as follows.
31+
if s[i:i + 2] in symbols_integer:
32+
integer = integer + symbols_integer[s[i:i + 2]]
33+
isPass = True
34+
35+
return integer

14.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def longestCommonPrefix(self, strs):
7+
"""
8+
:type strs: List[str]
9+
:rtype: str
10+
"""
11+
if not strs:
12+
return ""
13+
common_prefix = strs[0]
14+
for string in strs:
15+
min_len = min(len(string), len(common_prefix))
16+
mark = 0
17+
for i in range(min_len):
18+
mark = i
19+
if string[i] == common_prefix[i]:
20+
i += 1
21+
mark = i
22+
else:
23+
if i == 0:
24+
return ""
25+
break
26+
common_prefix = common_prefix[:mark]
27+
28+
return common_prefix

15.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def threeSum(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: List[List[int]]
10+
"""
11+
solution = []
12+
sorted_nums = sorted(nums)
13+
length = len(sorted_nums)
14+
for i in range(length):
15+
cur_num = sorted_nums[i]
16+
left = i + 1
17+
right = length - 1
18+
while left < right:
19+
if sorted_nums[left] + sorted_nums[right] + cur_num < 0:
20+
left += 1
21+
elif sorted_nums[left] + sorted_nums[right] + cur_num > 0:
22+
right -= 1
23+
else:
24+
triplet = [sorted_nums[left], cur_num, sorted_nums[right]]
25+
solution.append(sorted(triplet))
26+
left += 1
27+
right -= 1
28+
29+
solution_unique = []
30+
for sol in solution:
31+
if sol not in solution_unique:
32+
solution_unique.append(sol)
33+
return solution_unique

16.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def threeSumClosest(self, nums, target):
7+
"""
8+
:type nums: List[int]
9+
:type target: int
10+
:rtype: int
11+
"""
12+
nums.sort()
13+
min_distance = 2 ** 31 - 1
14+
length = len(nums)
15+
solution = 0 # keep the sum of three nums
16+
for i in range(length):
17+
cur_num = nums[i]
18+
left = i + 1
19+
right = length - 1
20+
while left < right:
21+
left_num = nums[left]
22+
right_num = nums[right]
23+
three_sum = cur_num + left_num + right_num
24+
25+
# the right point go back
26+
if three_sum > target:
27+
right -= 1
28+
if min_distance > three_sum - target:
29+
solution = three_sum
30+
min_distance = three_sum - target
31+
# the left point go forward
32+
elif three_sum < target:
33+
if min_distance > target - three_sum:
34+
solution = three_sum
35+
min_distance = target - three_sum
36+
left += 1
37+
else:
38+
return three_sum
39+
40+
return solution

17.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def letterCombinations(self, digits):
7+
"""
8+
:type digits: str
9+
:rtype: List[str]
10+
"""
11+
12+
phone_letters = {0: [" "],
13+
1: ["*"],
14+
2: ["a", "b", "c"],
15+
3: ["d", "e", "f"],
16+
4: ["g", "h", "i"],
17+
5: ["j", "k", "l"],
18+
6: ["m", "n", "o"],
19+
7: ["p", "q", "r", "s"],
20+
8: ["t", "u", "v"],
21+
9: ["w", "x", "y", "z"],
22+
}
23+
if digits:
24+
all_str = phone_letters[ord(digits[0]) - ord("0")]
25+
else:
26+
return []
27+
28+
for i in range(1, len(digits)):
29+
all_str = self.combination(all_str, phone_letters[ord(digits[i]) - ord("0")])
30+
31+
return all_str
32+
33+
# return string which combines a in str_a with b in str_b
34+
def combination(self, str_a, str_b):
35+
combine_str = []
36+
for a in str_a:
37+
for b in str_b:
38+
combine_str.append(a + b)
39+
40+
return combine_str

2.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
# Definition for singly-linked list.
6+
class ListNode(object):
7+
def __init__(self, x):
8+
self.val = x
9+
self.next = None
10+
11+
12+
class Solution(object):
13+
def addTwoNumbers(self, l1, l2):
14+
"""
15+
:type l1: ListNode
16+
:type l2: ListNode
17+
:rtype: ListNode
18+
"""
19+
carry_in = 0
20+
head = ListNode(0)
21+
l_sum = head
22+
23+
while l1 and l2:
24+
l_sum.next = ListNode((l1.val + l2.val + carry_in) % 10)
25+
carry_in = (l1.val + l2.val + carry_in) / 10
26+
l1 = l1.next
27+
l2 = l2.next
28+
l_sum = l_sum.next
29+
30+
if l1:
31+
while l1:
32+
l_sum.next = ListNode((l1.val + carry_in) % 10)
33+
carry_in = (l1.val + carry_in) / 10
34+
l1 = l1.next
35+
l_sum = l_sum.next
36+
37+
if l2:
38+
while l2:
39+
l_sum.next = ListNode((l2.val + carry_in) % 10)
40+
carry_in = (l2.val + carry_in) / 10
41+
l2 = l2.next
42+
l_sum = l_sum.next
43+
44+
if carry_in != 0:
45+
l_sum.next = ListNode(carry_in)
46+
47+
return head.next

3.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def lengthOfLongestSubstring(self, s):
7+
"""
8+
:type s: str
9+
:rtype: int
10+
"""
11+
12+
max_length = 0
13+
start = 1
14+
end = 1
15+
index = 1
16+
char_dict = {}
17+
18+
for char in s:
19+
if char in char_dict and start <= char_dict[char] < end:
20+
start = char_dict[char] + 1
21+
end = index + 1
22+
else:
23+
end = index + 1
24+
if end - start > max_length:
25+
max_length = end - start
26+
27+
char_dict[char] = index
28+
index = index + 1
29+
30+
return max_length

5.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
def longestPalindrome(s):
6+
"""
7+
:type s: str
8+
:rtype: str
9+
"""
10+
11+
length = len(s)
12+
palindrome_record = [[0 for col in range(length)] for row in range(length)]
13+
14+
start = 0
15+
end = 0
16+
# length of the palindrome is less than 2
17+
for i in range(length):
18+
palindrome_record[i][i] = 1
19+
20+
if i + 1 < length and s[i] == s[i + 1]:
21+
palindrome_record[i][i + 1] = 1
22+
start = i
23+
end = i + 1
24+
25+
# length of the palindrome is growing to len(s)
26+
for len_of_palidrome in range(3, length + 1):
27+
for i in range(length - len_of_palidrome + 1):
28+
j = i + len_of_palidrome - 1
29+
if palindrome_record[i + 1][j - 1] and s[i] == s[j]:
30+
palindrome_record[i][j] = 1
31+
start = i
32+
end = i + len_of_palidrome
33+
34+
return s[start: end + 1]
35+
36+
37+
if __name__ == "__main__":
38+
print longestPalindrome("abacdcaba")

0 commit comments

Comments
 (0)