Skip to content

Commit 19bfa12

Browse files
ch8
面试题65和66
1 parent 0da3829 commit 19bfa12

3 files changed

Lines changed: 133 additions & 81 deletions

File tree

.idea/workspace.xml

Lines changed: 49 additions & 81 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。
3+
例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,
4+
他们的最大值分别为{4,4,6,6,6,5};
5+
针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个:
6+
{[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。
7+
'''
8+
9+
# -*- coding:utf-8 -*-
10+
class Solution:
11+
def maxInWindows(self, num, size):
12+
if num == None or len(num) <= 0 or size <= 0:
13+
return []
14+
deque = []
15+
if len(num) >= size:
16+
index = []
17+
for i in range(size):
18+
while len(index) > 0 and num[i] > num[index[-1]]:
19+
index.pop()
20+
index.append(i)
21+
22+
for i in range(size, len(num)):
23+
deque.append(num[index[0]])
24+
while len(index) > 0 and num[i] >= num[index[-1]]:
25+
index.pop()
26+
if len(index) > 0 and index[0] <= i - size:
27+
index.pop(0)
28+
index.append(i)
29+
30+
deque.append(num[index[0]])
31+
return deque
32+
33+
test = [2, 3, 4, 2, 6, 2, 5, 1]
34+
s = Solution()
35+
print(s.maxInWindows(test, 3))
36+

Target Offer/矩阵中的路径.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'''
2+
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。
3+
路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。
4+
如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。
5+
例如 [[a b c e], [s f c s], [a d e e]] 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,
6+
因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
7+
'''
8+
9+
# -*- coding:utf-8 -*-
10+
class Solution:
11+
def hasPath(self, matrix, rows, cols, path):
12+
if matrix == None or rows < 1 or cols < 1 or path == None:
13+
return False
14+
visited = [0] * (rows * cols)
15+
16+
pathLength = 0
17+
for row in range(rows):
18+
for col in range(cols):
19+
if self.hasPathCore(matrix, rows, cols, row, col, path, pathLength, visited):
20+
return True
21+
return False
22+
23+
def hasPathCore(self, matrix, rows, cols, row, col, path, pathLength, visited):
24+
if len(path) == pathLength:
25+
return True
26+
27+
hasPath = False
28+
if row >= 0 and row < rows and col >= 0 and col < cols and matrix[row * cols + col] == path[pathLength] and not \
29+
visited[row * cols + col]:
30+
31+
pathLength += 1
32+
visited[row * cols + col] = True
33+
34+
hasPath = self.hasPathCore(matrix, rows, cols, row, col - 1, path, pathLength, visited) or \
35+
self.hasPathCore(matrix, rows, cols, row - 1, col, path, pathLength, visited) or \
36+
self.hasPathCore(matrix, rows, cols, row, col + 1, path, pathLength, visited) or \
37+
self.hasPathCore(matrix, rows, cols, row + 1, col, path, pathLength, visited)
38+
39+
if not hasPath:
40+
pathLength -= 1
41+
visited[row * cols + col] = False
42+
43+
return hasPath
44+
45+
s = Solution()
46+
ifTrue = s.hasPath("ABCESFCSADEE", 3, 4, "ABCCED")
47+
ifTrue2 = s.hasPath("ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS", 5, 8, "SGGFIECVAASABCEHJIGQEM")
48+
print(ifTrue2)

0 commit comments

Comments
 (0)