Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6bd7dc3

Browse filesBrowse files
committed
Update to 066
Update to 066
1 parent 483d19f commit 6bd7dc3
Copy full SHA for 6bd7dc3

File tree

3 files changed

+145
-0
lines changed
Filter options

3 files changed

+145
-0
lines changed

‎Python3/054_Spira_Matrix.py

Copy file name to clipboard
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
5+
6+
For example,
7+
Given the following matrix:
8+
9+
[
10+
[ 1, 2, 3 ],
11+
[ 4, 5, 6 ],
12+
[ 7, 8, 9 ]
13+
]
14+
You should return [1,2,3,6,9,8,7,4,5].
15+
'''
16+
17+
18+
class Solution(object):
19+
def spiralOrder(self, matrix):
20+
"""
21+
:type matrix: List[List[int]]
22+
:rtype: List[int]
23+
"""
24+
if not matrix:
25+
return []
26+
left = top = 0
27+
right = len(matrix[0]) - 1
28+
bottom = len(matrix) - 1
29+
result = []
30+
while left < right and top < bottom:
31+
for i in range(left, right):
32+
result.append(matrix[top][i])
33+
for i in range(top, bottom):
34+
result.append(matrix[i][right])
35+
for i in range(right, left, -1):
36+
result.append(matrix[bottom][i])
37+
for i in range(bottom, top, -1):
38+
result.append(matrix[i][left])
39+
left += 1
40+
right -= 1
41+
top += 1
42+
bottom -= 1
43+
if right == left and top == bottom:
44+
result.append(matrix[top][left])
45+
elif right == left:
46+
for i in range(top, bottom + 1):
47+
result.append(matrix[i][left])
48+
elif top == bottom:
49+
for i in range(left, right + 1):
50+
result.append(matrix[top][i])
51+
return result
52+
53+
54+
if __name__ == "__main__":
55+
assert Solution().spiralOrder([
56+
[1, 2, 3],
57+
[4, 5, 6],
58+
[7, 8, 9]
59+
]) == [1, 2, 3, 6, 9, 8, 7, 4, 5]
60+
assert Solution().spiralOrder([[2], [3]]) == [2, 3]
61+
assert Solution().spiralOrder([[2, 3]]) == [2, 3]

‎Python3/055_Jump_Game.py

Copy file name to clipboard
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Given an array of non-negative integers, you are initially positioned at the first index of the array.
5+
6+
Each element in the array represents your maximum jump length at that position.
7+
8+
Determine if you are able to reach the last index.
9+
10+
For example:
11+
A = [2,3,1,1,4], return true.
12+
13+
A = [3,2,1,0,4], return false.
14+
'''
15+
16+
17+
class Solution(object):
18+
def canJump(self, nums):
19+
"""
20+
:type nums: List[int]
21+
:rtype: bool
22+
"""
23+
if not nums:
24+
return False
25+
lenth = len(nums)
26+
index = 0
27+
longest = nums[0]
28+
while index <= longest:
29+
if longest >= lenth - 1:
30+
return True
31+
longest = max(longest, index + nums[index])
32+
index += 1
33+
return False
34+
35+
36+
if __name__ == "__main__":
37+
assert Solution().canJump([2, 3, 1, 1, 4]) == True
38+
assert Solution().canJump([3, 2, 1, 0, 4]) == False

‎Python3/056_Merge_Intervals.py

Copy file name to clipboard
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Given a collection of intervals, merge all overlapping intervals.
5+
6+
For example,
7+
Given [1,3],[2,6],[8,10],[15,18],
8+
return [1,6],[8,10],[15,18].
9+
'''
10+
11+
12+
# Definition for an interval.
13+
class Interval(object):
14+
def __init__(self, s=0, e=0):
15+
self.start = s
16+
self.end = e
17+
18+
# To print the result
19+
def __str__(self):
20+
return "[" + str(self.start) + "," + str(self.end) + "]"
21+
22+
23+
class Solution(object):
24+
def merge(self, intervals):
25+
"""
26+
:type intervals: List[Interval]
27+
:rtype: List[Interval]
28+
"""
29+
result = []
30+
if not intervals:
31+
return result
32+
intervals.sort(key = lambda x : x.start)
33+
result.append(intervals[0])
34+
for interval in intervals[1:]:
35+
prev = result[-1]
36+
if prev.end >= interval.start:
37+
prev.end = max(prev.end, interval.end)
38+
else:
39+
result.append(interval)
40+
return result
41+
42+
43+
if __name__ == "__main__":
44+
intervals = Solution().merge([Interval(1, 3), Interval(2, 6), Interval(8, 10), Interval(15, 18)])
45+
for interval in intervals:
46+
print(interval)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.