Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit6bd7dc3

Browse files
committed
Update to 066
Update to 066
1 parent483d19f commit6bd7dc3

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

‎Python3/054_Spira_Matrix.py

Lines 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+
classSolution(object):
19+
defspiralOrder(self,matrix):
20+
"""
21+
:type matrix: List[List[int]]
22+
:rtype: List[int]
23+
"""
24+
ifnotmatrix:
25+
return []
26+
left=top=0
27+
right=len(matrix[0])-1
28+
bottom=len(matrix)-1
29+
result= []
30+
whileleft<rightandtop<bottom:
31+
foriinrange(left,right):
32+
result.append(matrix[top][i])
33+
foriinrange(top,bottom):
34+
result.append(matrix[i][right])
35+
foriinrange(right,left,-1):
36+
result.append(matrix[bottom][i])
37+
foriinrange(bottom,top,-1):
38+
result.append(matrix[i][left])
39+
left+=1
40+
right-=1
41+
top+=1
42+
bottom-=1
43+
ifright==leftandtop==bottom:
44+
result.append(matrix[top][left])
45+
elifright==left:
46+
foriinrange(top,bottom+1):
47+
result.append(matrix[i][left])
48+
eliftop==bottom:
49+
foriinrange(left,right+1):
50+
result.append(matrix[top][i])
51+
returnresult
52+
53+
54+
if__name__=="__main__":
55+
assertSolution().spiralOrder([
56+
[1,2,3],
57+
[4,5,6],
58+
[7,8,9]
59+
])== [1,2,3,6,9,8,7,4,5]
60+
assertSolution().spiralOrder([[2], [3]])== [2,3]
61+
assertSolution().spiralOrder([[2,3]])== [2,3]

‎Python3/055_Jump_Game.py

Lines 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+
classSolution(object):
18+
defcanJump(self,nums):
19+
"""
20+
:type nums: List[int]
21+
:rtype: bool
22+
"""
23+
ifnotnums:
24+
returnFalse
25+
lenth=len(nums)
26+
index=0
27+
longest=nums[0]
28+
whileindex<=longest:
29+
iflongest>=lenth-1:
30+
returnTrue
31+
longest=max(longest,index+nums[index])
32+
index+=1
33+
returnFalse
34+
35+
36+
if__name__=="__main__":
37+
assertSolution().canJump([2,3,1,1,4])==True
38+
assertSolution().canJump([3,2,1,0,4])==False

‎Python3/056_Merge_Intervals.py

Lines 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+
classInterval(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+
classSolution(object):
24+
defmerge(self,intervals):
25+
"""
26+
:type intervals: List[Interval]
27+
:rtype: List[Interval]
28+
"""
29+
result= []
30+
ifnotintervals:
31+
returnresult
32+
intervals.sort(key=lambdax :x.start)
33+
result.append(intervals[0])
34+
forintervalinintervals[1:]:
35+
prev=result[-1]
36+
ifprev.end>=interval.start:
37+
prev.end=max(prev.end,interval.end)
38+
else:
39+
result.append(interval)
40+
returnresult
41+
42+
43+
if__name__=="__main__":
44+
intervals=Solution().merge([Interval(1,3),Interval(2,6),Interval(8,10),Interval(15,18)])
45+
forintervalinintervals:
46+
print(interval)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp