Skip to content

Commit 4d45683

Browse files
committedJun 16, 2019
2019-06-16
1 parent bc02a89 commit 4d45683

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
 

‎1089.复写零/1089-复写零.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def duplicateZeros(self, arr):
3+
"""
4+
:type arr: List[int]
5+
:rtype: None Do not return anything, modify arr in-place instead.
6+
"""
7+
l = len(arr)
8+
i = 0
9+
while i < l:
10+
if arr[i] == 0:
11+
for j in range(l - 1, i + 1, -1):
12+
arr[j] = arr[j - 1]
13+
if i + 1 < l:
14+
arr[i + 1] = 0
15+
i += 2
16+
else:
17+
i += 1
18+
return arr
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from heapq import *
2+
class Solution(object):
3+
def largestValsFromLabels(self, values, labels, num_wanted, use_limit):
4+
"""
5+
:type values: List[int]
6+
:type labels: List[int]
7+
:type num_wanted: int
8+
:type use_limit: int
9+
:rtype: int
10+
"""
11+
record = dict()
12+
l = len(values)
13+
for i in range(l):
14+
if labels[i] not in record:
15+
record[labels[i]] = [values[i]]
16+
heapify(record[labels[i]])
17+
else:
18+
heappush(record[labels[i]], values[i])
19+
if len(record[labels[i]]) > use_limit:
20+
heappop(record[labels[i]])
21+
22+
res = []
23+
for key, val in record.items():
24+
res += val
25+
res.sort()
26+
return sum(res[-num_wanted:])
27+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from collections import deque
2+
class Solution(object):
3+
def shortestPathBinaryMatrix(self, grid):
4+
"""
5+
:type grid: List[List[int]]
6+
:rtype: int
7+
"""
8+
n = len(grid)
9+
# print n
10+
11+
if grid[0][0] or grid[-1][-1] == 1:
12+
return -1
13+
queue = deque([[[0, 0], 1]])
14+
# queue2 = deque([[[n -1 , n - 1], 1]])
15+
visited = set((0,0))
16+
dx = [1, -1, 0, 0, 1, -1, -1, 1]
17+
dy = [0, 0, 1, -1, -1, 1, -1, 1]
18+
cnt = 1
19+
record = dict()
20+
while queue:
21+
cur, cnt = queue.popleft()
22+
# print cur, cnt
23+
x0, y0 = cur[0], cur[1]
24+
25+
if x0 == n - 1 and y0 == n - 1:
26+
return cnt
27+
for k in range(8):
28+
x = x0 + dx[k]
29+
y = y0 + dy[k]
30+
31+
if 0 <= x <n and 0 <= y < n and grid[x][y] == 0 and (x, y) not in visited:
32+
visited.add((x, y))
33+
queue.append([[x, y], cnt + 1])
34+
return -1
35+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution(object):
2+
def shortestCommonSupersequence(self, str1, str2):
3+
"""
4+
:type str1: str
5+
:type str2: str
6+
:rtype: str
7+
"""
8+
#先找到最长公共子序列
9+
l1, l2 = len(str1), len(str2)
10+
if l1 < l2:
11+
l1, l2 = l2, l1
12+
str1, str2 = str2, str1
13+
common = self.findLCS(str1, str2)
14+
# print common
15+
res = ""
16+
str1_idx = 0
17+
str2_idx = 0
18+
for char in common:
19+
while str1_idx < l1 and str1[str1_idx] != char:
20+
res += str1[str1_idx]
21+
str1_idx += 1
22+
while str2_idx < l2 and str2[str2_idx] != char:
23+
res += str2[str2_idx]
24+
str2_idx += 1
25+
res += char
26+
str1_idx += 1
27+
str2_idx += 1
28+
29+
if str1_idx != l1:
30+
res += str1[str1_idx:]
31+
if str2_idx != l2:
32+
res += str2[str2_idx:]
33+
return res
34+
35+
def findLCS(self, str1, str2):
36+
m, n = len(str1), len(str2)
37+
dp = [["" for _ in range(n + 1)] for _ in range(m + 1)]
38+
for i in range(1, m + 1):
39+
for j in range(1, n + 1):
40+
if str1[i - 1] == str2[j - 1]: #匹配上了
41+
dp[i][j] = dp[i - 1][j - 1] + str1[i - 1]
42+
elif len(dp[i - 1][j]) > len(dp[i][j - 1]):
43+
dp[i][j] = dp[i - 1][j]
44+
elif len(dp[i - 1][j]) <= len(dp[i][j - 1]):
45+
dp[i][j] = dp[i][j - 1]
46+
47+
return dp[m][n]

0 commit comments

Comments
 (0)