Skip to content

Commit a6d9be5

Browse files
committed
Solution of 3, 6, 17, 19, 22, 23, 24 problems
1 parent 6d949f9 commit a6d9be5

File tree

8 files changed

+294
-0
lines changed

8 files changed

+294
-0
lines changed

03.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Given a string, find the length of the longest substring without repeating characters.
3+
4+
Examples:
5+
6+
Given "abcabcbb", the answer is "abc", which the length is 3.
7+
8+
Given "bbbbb", the answer is "b", with the length of 1.
9+
10+
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
11+
'''
12+
13+
class Solution(object):
14+
def lengthOfLongestSubstring(self, s):
15+
"""
16+
:type s: str
17+
:rtype: int
18+
"""
19+
mapSet = {}
20+
start, result = 0, 0
21+
22+
for end in range(len(s)):
23+
if s[end] in mapSet:
24+
start = max(mapSet[s[end]], start)
25+
result = max(result, end-start+1)
26+
mapSet[s[end]] = end+1
27+
28+
return result

5.py renamed to 05.py

File renamed without changes.

06.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
3+
4+
P A H N
5+
A P L S I I G
6+
Y I R
7+
8+
And then read line by line: "PAHNAPLSIIGYIR"
9+
'''
10+
11+
class Solution(object):
12+
def convert(self, s, numRows):
13+
"""
14+
:type s: str
15+
:type numRows: int
16+
:rtype: str
17+
"""
18+
19+
if numRows == 1:
20+
return s
21+
22+
result = ["" for _ in range(numRows)]
23+
row, down = 0, 1
24+
for char in s:
25+
result[row] += char
26+
27+
if row == numRows - 1:
28+
down = 0
29+
if row == 0:
30+
down = 1
31+
32+
if down:
33+
row += 1
34+
else:
35+
row -= 1
36+
final_string = ""
37+
for value in result:
38+
final_string += value
39+
return final_string
40+
41+
print Solution().convert("PAYPALISHIRING",3)

17.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
3+
4+
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
5+
'''
6+
7+
class Solution(object):
8+
def letterCombinations(self, digits):
9+
"""
10+
:type digits: str
11+
:rtype: List[str]
12+
"""
13+
14+
phoneMap = { '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7' : 'pqrs', '8': 'tuv', '9':'wxyz'}
15+
number = str(digits)
16+
17+
if number == "":
18+
return []
19+
20+
result = ['']
21+
for char in number:
22+
values = phoneMap[char]
23+
new_result = []
24+
for prefix in result:
25+
currElement = prefix
26+
for value in values:
27+
new_result.append(currElement+value)
28+
29+
result = new_result
30+
# result = [prefix+value for prefix in result for value in values]
31+
return result
32+
33+
print Solution().letterCombinations("23")

19.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
Given a linked list, remove the n-th node from the end of list and return its head.
3+
4+
Example:
5+
6+
Given linked list: 1->2->3->4->5, and n = 2.
7+
8+
After removing the second node from the end, the linked list becomes 1->2->3->5.
9+
10+
'''
11+
12+
# Definition for singly-linked list.
13+
# class ListNode(object):
14+
# def __init__(self, x):
15+
# self.val = x
16+
# self.next = None
17+
18+
class Solution(object):
19+
def removeNthFromEnd(self, head, n):
20+
"""
21+
:type head: ListNode
22+
:type n: int
23+
:rtype: ListNode
24+
"""
25+
if not head:
26+
return None
27+
28+
ref = head
29+
while n > 0:
30+
ref = ref.next
31+
n -= 1
32+
33+
if ref is None:
34+
return head.next
35+
else:
36+
main = head
37+
while ref.next:
38+
main = main.next
39+
ref = ref.next
40+
41+
main.next = main.next.next
42+
return head

22.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
3+
4+
For example, given n = 3, a solution set is:
5+
6+
[
7+
"((()))",
8+
"(()())",
9+
"(())()",
10+
"()(())",
11+
"()()()"
12+
]
13+
'''
14+
15+
class Solution(object):
16+
def generateParenthesis(self, n):
17+
"""
18+
:type n: int
19+
:rtype: List[str]
20+
"""
21+
22+
result = []
23+
24+
def backtracking(S, left, right):
25+
if len(S) == 2*n:
26+
result.append(S)
27+
return
28+
29+
if left < n:
30+
backtracking(S+'(', left+1, right)
31+
32+
if right < left:
33+
backtracking(S+')', left, right+1)
34+
35+
backtracking('', 0, 0)
36+
return result

23.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'''
2+
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
3+
4+
Example:
5+
6+
Input:
7+
[
8+
1->4->5,
9+
1->3->4,
10+
2->6
11+
]
12+
Output: 1->1->2->3->4->4->5->6
13+
14+
'''
15+
16+
class Solution(object):
17+
def mergeKLists(self, lists):
18+
"""
19+
:type lists: List[ListNode]
20+
:rtype: ListNode
21+
"""
22+
23+
from heapq import heappush, heappop
24+
25+
heap = []
26+
head = point = ListNode(0)
27+
for element in lists:
28+
if element:
29+
heapq.heappush(heap, (element.val, element))
30+
31+
while heap:
32+
value, node = heapq.heappop(heap)
33+
head.next = ListNode(value)
34+
head = head.next
35+
node = node.next
36+
if node:
37+
heapq.heappush(heap, (node.val, node))
38+
39+
return point.next
40+
41+
# Space: O(K)
42+
# Time: O(N*log(K))
43+
44+
class Solution(object):
45+
def mergeKLists(self, lists):
46+
"""
47+
:type lists: List[ListNode]
48+
:rtype: ListNode
49+
"""
50+
51+
def merge2Lists(l1, l2):
52+
head = point = ListNode(0)
53+
while l1 and l2:
54+
if l1.val <= l2.val:
55+
point.next = ListNode(l1.val)
56+
l1 = l1.next
57+
else:
58+
point.next = ListNode(l2.val)
59+
l2 = l2.next
60+
point = point.next
61+
62+
if l1:
63+
point.next = l1
64+
else:
65+
point.next = l2
66+
return head.next
67+
68+
if not lists:
69+
return lists
70+
71+
interval = 1
72+
while interval < len(lists):
73+
for index in range(0, len(lists) - interval ,interval*2):
74+
lists[index] = merge2Lists(lists[index], lists[index+interval])
75+
76+
interval *= 2
77+
78+
return lists[0]
79+
80+
81+
82+
# Time: O(N*log(k))
83+
# Space: O(1)

24.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Given a linked list, swap every two adjacent nodes and return its head.
3+
4+
Example:
5+
6+
Given 1->2->3->4, you should return the list as 2->1->4->3.
7+
'''
8+
9+
# Definition for singly-linked list.
10+
# class ListNode(object):
11+
# def __init__(self, x):
12+
# self.val = x
13+
# self.next = None
14+
15+
class Solution(object):
16+
def swapPairs(self, head):
17+
"""
18+
:type head: ListNode
19+
:rtype: ListNode
20+
"""
21+
22+
if head is None:
23+
return head
24+
25+
ref = head
26+
27+
while ref is not None and ref.next is not None:
28+
ref.val, ref.next.val = ref.next.val, ref.val
29+
ref = ref.next.next
30+
31+
return head

0 commit comments

Comments
 (0)