Skip to content

Commit ee490c1

Browse files
committed
string probs
1 parent 3e9cc34 commit ee490c1

File tree

6 files changed

+221
-30
lines changed

6 files changed

+221
-30
lines changed

array/plus_one.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44
# The digits are stored such that the most significant
55
# digit is at the head of the list.
66

7-
# def plusOne(digits):
8-
# """
9-
# :type digits: List[int]
10-
# :rtype: List[int]
11-
# """
12-
# digits[-1] = digits[-1] + 1
13-
# res = []
14-
# ten = 0
15-
# i = len(digits)-1
16-
# while i >= 0 or ten == 1:
17-
# sum = 0
18-
# if i >= 0:
19-
# sum += digits[i]
20-
# if ten:
21-
# sum += 1
22-
# res.append(sum % 10)
23-
# ten = sum / 10
24-
# i -= 1
25-
# return res[::-1]
7+
def plusOne(digits):
8+
"""
9+
:type digits: List[int]
10+
:rtype: List[int]
11+
"""
12+
digits[-1] = digits[-1] + 1
13+
res = []
14+
ten = 0
15+
i = len(digits)-1
16+
while i >= 0 or ten == 1:
17+
sum = 0
18+
if i >= 0:
19+
sum += digits[i]
20+
if ten:
21+
sum += 1
22+
res.append(sum % 10)
23+
ten = sum / 10
24+
i -= 1
25+
return res[::-1]
2626

2727

2828
def plus_one(digits):

matrix/pacific_atlantic.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Given an m x n matrix of non-negative integers representing
2+
# the height of each unit cell in a continent,
3+
# the "Pacific ocean" touches the left and top edges of the matrix
4+
# and the "Atlantic ocean" touches the right and bottom edges.
5+
6+
# Water can only flow in four directions (up, down, left, or right)
7+
# from a cell to another one with height equal or lower.
8+
9+
# Find the list of grid coordinates where water can flow to both the
10+
# Pacific and Atlantic ocean.
11+
12+
# Note:
13+
# The order of returned grid coordinates does not matter.
14+
# Both m and n are less than 150.
15+
# Example:
16+
17+
# Given the following 5x5 matrix:
18+
19+
# Pacific ~ ~ ~ ~ ~
20+
# ~ 1 2 2 3 (5) *
21+
# ~ 3 2 3 (4) (4) *
22+
# ~ 2 4 (5) 3 1 *
23+
# ~ (6) (7) 1 4 5 *
24+
# ~ (5) 1 1 2 4 *
25+
# * * * * * Atlantic
26+
27+
# Return:
28+
29+
# [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]]
30+
# (positions with parentheses in above matrix).
31+
32+
def pacific_atlantic(matrix):
33+
"""
34+
:type matrix: List[List[int]]
35+
:rtype: List[List[int]]
36+
"""
37+
n = len(matrix)
38+
if not n: return []
39+
m = len(matrix[0])
40+
if not m: return []
41+
res = []
42+
atlantic = [[False for _ in range (n)] for _ in range(m)]
43+
pacific = [[False for _ in range (n)] for _ in range(m)]
44+
for i in range(n):
45+
DFS(pacific, matrix, float("-inf"), i, 0)
46+
DFS(atlantic, matrix, float("-inf"), i, m-1)
47+
for i in range(m):
48+
DFS(pacific, matrix, float("-inf"), 0, i)
49+
DFS(atlantic, matrix, float("-inf"), n-1, i)
50+
for i in range(n):
51+
for j in range(m):
52+
if pacific[i][j] and atlantic[i][j]:
53+
res.append([i, j])
54+
return res
55+
56+
def DFS(grid, matrix, height, i, j):
57+
if i < 0 or i >= len(matrix) or j < 0 or j >= len(matrix[0]):
58+
return
59+
if grid[i][j] or matrix[i][j] < height:
60+
return
61+
grid[i][j] = True
62+
DFS(grid, matrix, matrix[i][j], i-1, j)
63+
DFS(grid, matrix, matrix[i][j], i+1, j)
64+
DFS(grid, matrix, matrix[i][j], i, j-1)
65+
DFS(grid, matrix, matrix[i][j], i, j+1)

queue/reconstruct_queue.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Suppose you have a random list of people standing in a queue.
2+
# Each person is described by a pair of integers (h, k),
3+
# where h is the height of the person and k is the number of people
4+
# in front of this person who have a height greater than or equal to h.
5+
# Write an algorithm to reconstruct the queue.
6+
7+
# Note:
8+
# The number of people is less than 1,100.
9+
10+
# Example
11+
12+
# Input:
13+
# [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
14+
15+
# Output:
16+
# [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
17+
18+
def reconstruct_queue(people):
19+
"""
20+
:type people: List[List[int]]
21+
:rtype: List[List[int]]
22+
"""
23+
queue = []
24+
people.sort(key=lambda x: (-x[0], x[1]))
25+
for h, k in people:
26+
queue.insert(k, (h, k))
27+
return queue
28+
29+

string/reverse_vowel.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11

2-
def isVowel(c):
3-
pass
4-
5-
def reverseVowel(s):
6-
i, j = 0, len(s)
2+
def reverse_vowel(s):
3+
vowels = "AEIOUaeiou"
4+
i, j = 0, len(s)-1
5+
s = list(s)
76
while i < j:
7+
while i < j and s[i] not in vowels:
8+
i += 1
9+
while i < j and s[j] not in vowels:
10+
j -= 1
11+
s[i], s[j] = s[j], s[i]
12+
i, j = i + 1, j - 1
13+
return "".join(s)
814

15+
test = "hello"
16+
print(test)
17+
print(reverse_vowel(test))

string/reverse_words.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11

2-
3-
4-
def reverse(array):
5-
i, j = 0, len(array)
2+
def reverse(array, i, j):
63
while i < j:
74
array[i], array[j] = array[j], array[i]
85
i += 1
96
j -= 1
107

11-
def reverseWords(array):
12-
reverse(array)
8+
def reverse_words(string):
9+
arr = list(string)
10+
n = len(arr)
11+
reverse(arr, 0, n-1)
12+
13+
start = None
14+
for i in range(n-1):
15+
if arr[i] == " ":
16+
if start:
17+
reverse(arr, start, i-1)
18+
start = None
19+
elif i == n-1:
20+
if start:
21+
reverse(arr, start, i)
22+
else:
23+
if not start:
24+
start = i
25+
return "".join(arr)
26+
27+
test = "I am keon kim and I like pizza"
28+
print(test)
29+
print(reverse_words(test))
30+
31+
1332

1433

tree/is_subtree.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Given two binary trees s and t, check if t is a subtree of s.
2+
# A subtree of a tree t is a tree consisting of a node in t and
3+
# all of its descendants in t.
4+
5+
# Example 1:
6+
7+
# Given s:
8+
9+
# 3
10+
# / \
11+
# 4 5
12+
# / \
13+
# 1 2
14+
15+
# Given t:
16+
17+
# 4
18+
# / \
19+
# 1 2
20+
# Return true, because t is a subtree of s.
21+
22+
# Example 2:
23+
24+
# Given s:
25+
26+
# 3
27+
# / \
28+
# 4 5
29+
# / \
30+
# 1 2
31+
# /
32+
# 0
33+
34+
# Given t:
35+
36+
# 3
37+
# /
38+
# 4
39+
# / \
40+
# 1 2
41+
# Return false, because even though t is part of s,
42+
# it does not contain all descendants of t.
43+
44+
# Follow up:
45+
# What if one tree is significantly lager than the other?
46+
47+
48+
def is_subtree(big, small):
49+
flag = False
50+
queue = collections.deque()
51+
queue.append(big)
52+
while queue:
53+
node = queue.popleft()
54+
if node.val == small.val:
55+
flag = comp(node, small)
56+
break
57+
else:
58+
queue.append(node.left)
59+
queue.append(node.right)
60+
return flag
61+
62+
def comp(p, q):
63+
if not p and not q:
64+
return True
65+
if p and q:
66+
return p.val == q.val and comp(p.left,q.left) and comp(p.right, q.right)
67+
return False
68+
69+

0 commit comments

Comments
 (0)