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

Commit dac0734

Browse files
2020年09月20日
1 parent 33c91a9 commit dac0734

File tree

7 files changed

+94
-67
lines changed

7 files changed

+94
-67
lines changed

‎0012.整数转罗马数字/0012-整数转罗马数字.py‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ def intToRoman(self, num):
44
:type num: int
55
:rtype: str
66
"""
7-
digit = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
8-
mapping = {1000:"M", 900:"CM", 500:"D",400:"CD", 100:"C", 90: "XC", 50:"L",40: "XL", 10:"X", 9:"IX", 5:"V", 4:"IV", 1:"I"}
7+
l = [(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), \
8+
(100, "C"), (90, "XC"), (50, "L"), (40, "XL"), (10, "X"), \
9+
(9, "IX"), (5, "V"), (4, "IV"), (1, "I")][::-1]
910
res = ""
10-
for i in digit:
11-
res += (num / i) * mapping[i]
12-
num -= i * (num / i)
13-
if num == 0:
14-
break
15-
return res
16-
11+
while num:
12+
for value, ch in l[::-1]:
13+
if num >= value:
14+
res += ch
15+
num -= value
16+
break
17+
else:
18+
l.pop()
19+
return res

‎0013.罗马数字转整数/0013-罗马数字转整数.py‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,15 @@ def romanToInt(self, s):
44
:type s: str
55
:rtype: int
66
"""
7-
dic = {"I": 1, "V":5, "X": 10, "L":50, "C":100, "D": 500, "M": 1000}
8-
stack = []
7+
dic = {"I":1, "V": 5, "X":10, "L":50, "C":100, "D":500, "M":1000}
98
res = 0
10-
for inx, item in enumerate(s):
11-
res += dic[item]
12-
# print res
13-
# s.append(item)
14-
if item == "V" or item == "X":
15-
if stack and stack[-1] == "I":
16-
res -= 2
17-
elif item == "L" or item == "C":
18-
if stack and stack[-1] == "X":
19-
res -= 20
20-
elif item == "D" or item == "M":
21-
if stack and stack[-1] == "C":
22-
res -= 200
23-
stack.append(item)
24-
return res
9+
pre_value = None
10+
for ch in s:
11+
if (ch in ["V", "X"] and pre_value == 1) or \
12+
(ch in ["L", "C"] and pre_value == 10) or \
13+
(ch in ["D", "M"] and pre_value == 100):
14+
res += dic[ch] - 2 * pre_value
15+
else:
16+
res += dic[ch]
17+
pre_value = dic[ch]
18+
return res

‎0256.粉刷房子/0256-粉刷房子.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ def minCost(self, costs):
1010
costs[i][0] += min(costs[i - 1][1], costs[i - 1][2])
1111
costs[i][1] += min(costs[i - 1][0], costs[i - 1][2])
1212
costs[i][2] += min(costs[i - 1][0], costs[i - 1][1])
13-
return min(costs[-1])
13+
return min(costs[-1])
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def eraseOverlapIntervals(self, intervals):
3+
"""
4+
:type intervals: List[List[int]]
5+
:rtype: int
6+
"""
7+
if not intervals or not intervals[0]:
8+
return 0
9+
10+
intervals = sorted(intervals, key = lambda x:x[1])
11+
pre_end = intervals[0][1]
12+
canAttendCnt = 1
13+
for i in range(1, len(intervals)):
14+
if intervals[i][0] >= pre_end: # start later than previous one end
15+
canAttendCnt += 1
16+
pre_end = intervals[i][1]
17+
return len(intervals) - canAttendCnt
18+

‎0452.用最少数量的箭引爆气球/0452-用最少数量的箭引爆气球.py‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ def findMinArrowShots(self, points):
77
if not points or not points[0]:
88
return 0
99

10-
points.sort(key = lambda x:x[1])
11-
12-
arrow = points[0][1]
13-
10+
points = sorted(points, key = lambda x: x[1])
1411
res = 1
15-
for point in points:
16-
if arrow < point[0]:
12+
pre_end = points[0][1]
13+
14+
for i in range(1, len(points)):
15+
if points[i][0] > pre_end:
1716
res += 1
18-
arrow = point[1]
19-
20-
return res
17+
pre_end = points[i][1]
18+
return res

‎0542.01矩阵/0542-01矩阵.py‎

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,35 @@
1-
from collections import deque
21
class Solution(object):
32
def updateMatrix(self, matrix):
43
"""
54
:type matrix: List[List[int]]
65
:rtype: List[List[int]]
76
"""
7+
from collections import deque
88
if not matrix or not matrix[0]:
9-
return matrix
9+
return matrix
1010
m, n = len(matrix), len(matrix[0])
11-
res = matrix[:]
12-
13-
q = deque()
14-
15-
for i in range(m):
16-
for j in range(n):
17-
if matrix[i][j] == 0:
18-
q.append([[i, j], 0])
1911

20-
dx = [1, -1, 0, 0]
12+
dx = [1, -1, 0, 0]
2113
dy = [0, 0, 1, -1]
22-
visited = [[0 for _ in range(n+1)] for _ in range(m+1)]
23-
24-
whileq:
25-
tmp, distance=q.popleft()
26-
x0, y0=tmp[0], tmp[1]
27-
28-
ifmatrix[x0][y0] ==1:
29-
res[x0][y0] =distance
14+
res = [[0 for _ in range(n)] for _ in range (m)]
15+
foriinrange(m):
16+
forjinrange(n):
17+
ifmatrix[i][j] ==1:
18+
queue=deque([(i, j, 0)])
19+
visited=set((i, j))
20+
whilequeue:
21+
x, y, dist=queue.popleft()
3022

31-
for k in range(4):
32-
x = x0 + dx[k]
33-
y = y0 + dy[k]
23+
if matrix[x][y] == 0:
24+
res[i][j] = dist
25+
break
26+
else:
27+
for k in range(4):
28+
xx = x + dx[k]
29+
yy = y + dy[k]
3430

35-
if 0 <= x < m and 0 <= y < n and visited[x][y] != 1:
36-
q.append([[x, y], distance + 1])
37-
visited[x][y] = 1
38-
return res
39-
40-
41-
42-
43-
31+
if 0 <= xx < m and 0 <= yy < n and (xx, yy) not in visited:
32+
visited.add((xx, yy))
33+
queue.append((xx, yy, dist + 1))
34+
35+
return res

‎0881.救生艇/0881-救生艇.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def numRescueBoats(self, people, limit):
3+
"""
4+
:type people: List[int]
5+
:type limit: int
6+
:rtype: int
7+
"""
8+
people.sort()
9+
left, right = 0, len(people) - 1
10+
res = 0
11+
while left <= right:
12+
if left == right:
13+
res += 1
14+
break
15+
if people[left] + people[right] <= limit:
16+
left += 1
17+
right -= 1
18+
res += 1
19+
else:
20+
right -= 1
21+
res += 1
22+
return res

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /