Skip to content

Commit b079f2a

Browse files
committed
2020-07-11
1 parent 6ef504c commit b079f2a

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution(object):
2+
def reformatDate(self, date):
3+
"""
4+
:type date: str
5+
:rtype: str
6+
"""
7+
date = date.split(" ")
8+
9+
day = "0" + date[0][:1] if len(date[0]) == 3 else date[0][:2]
10+
mon = {"Jan":"01", "Feb":"02", "Mar":"03", "Apr":"04", "May":"05", "Jun":"06",
11+
"Jul":"07", "Aug":"08", "Sep":"09", "Oct":"10", "Nov":"11", "Dec":"12"}[date[1]]
12+
year = date[2]
13+
return "-".join([year, mon, day])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def rangeSum(self, nums, n, left, right):
3+
"""
4+
:type nums: List[int]
5+
:type n: int
6+
:type left: int
7+
:type right: int
8+
:rtype: int
9+
"""
10+
res = []
11+
for i in range(n):
12+
for j in range(i + 1, n + 1):
13+
res.append(sum(nums[i:j]))
14+
res.sort()
15+
# print res
16+
return sum(res[left - 1:right]) % (10 ** 9 + 7)
17+
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def minDifference(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
if len(nums) <= 4:
8+
return 0
9+
nums.sort()
10+
11+
# 1. 去前三个
12+
res = nums[-1] - nums[3]
13+
# 2. 去前两个和最后一个
14+
res = min(res, nums[-2] - nums[2])
15+
# 3. 去第一个和最后两个
16+
res = min(res, nums[-3] - nums[1])
17+
# 4. 去最后三个
18+
res = min(res, nums[-4] - nums[0])
19+
return res
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def winnerSquareGame(self, n):
3+
"""
4+
:type n: int
5+
:rtype: bool
6+
"""
7+
dp = [0 for _ in range(n + 1)]
8+
dp[1] = 1
9+
for i in range(2, n + 1):
10+
for j in range(1, int(i ** 0.5 + 1)):
11+
if not dp[i - j * j]:
12+
dp[i] = 1
13+
break
14+
return dp[n] == 1
15+
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def calculateMinimumHP(self, dungeon):
3+
"""
4+
:type dungeon: List[List[int]]
5+
:rtype: int
6+
"""
7+
if len(dungeon[0])==0:
8+
return 1
9+
dungeon[-1][-1] = max(1,1-dungeon[-1][-1])
10+
# 边界
11+
for i in range(len(dungeon)-2,-1,-1):
12+
dungeon[i][-1] = max(1,dungeon[i+1][-1]-dungeon[i][-1])
13+
for j in range(len(dungeon[0])-2,-1,-1):
14+
dungeon[-1][j] = max(1,dungeon[-1][j+1]-dungeon[-1][j])
15+
16+
for i in range(len(dungeon)-2,-1,-1):
17+
for j in range(len(dungeon[0])-2,-1,-1):
18+
dungeon[i][j] = max(1,min(dungeon[i][j+1]-dungeon[i][j],dungeon[i+1][j]-dungeon[i][j]))
19+
# print(dungeon)
20+
return dungeon[0][0]

0 commit comments

Comments
 (0)