Skip to content

Commit 908534f

Browse files
committed
2019-08-15
1 parent 054360f commit 908534f

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def dayOfYear(self, date):
3+
"""
4+
:type date: str
5+
:rtype: int
6+
"""
7+
days1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
8+
days2 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
9+
10+
Date = date.split("-")
11+
year, month, day = int(Date[0]), int(Date[1]), int(Date[2])
12+
13+
if not year % 400 or (not year % 4 and year % 100):
14+
return sum(days2[:month - 1]) + day
15+
return sum(days1[:month - 1]) + day
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
def numRollsToTarget(self, d, f, target):
3+
"""
4+
:type d: int
5+
:type f: int
6+
:type target: int
7+
:rtype: int
8+
"""
9+
record = dict()
10+
def backtrack(dice, face, t):
11+
if (dice, face, t) in record: #如果已经知道当前问题的解
12+
return record[(dice, face, t)] #直接读取即可
13+
14+
if dice == 0: #当没有骰子的时候,判断一下是不是刚好找到target
15+
return 1 if t == 0 else 0
16+
17+
if t < 0 or dice <= 0: #无效的时候没有解,所以返回0
18+
return 0
19+
tmp = 0 #tmp用于记录当前问题有多少个解
20+
for i in range(1, face + 1): #尝试所有的组合
21+
tmp += backtrack(dice - 1, face, t - i)
22+
23+
record[(dice, face, t)] = tmp #把解存起来,方便以后用
24+
return tmp
25+
26+
backtrack(d, f, target)
27+
return max(record.values()) % (10 ** 9 + 7) #最大的解就是母问题的解
28+
29+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution(object):
2+
def maxRepOpt1(self, text):
3+
"""
4+
:type text: str
5+
:rtype: int
6+
"""
7+
if len(text) == text.count(text[0]):
8+
return len(text)
9+
record = collections.Counter(text)
10+
start, end = 0, 1
11+
cur, nxt, idx_nxt = text[0], None, 0
12+
res = 1
13+
while end < len(text):
14+
if text[end] != cur :
15+
if nxt is None:
16+
nxt = text[end] #找到了第一个异字符
17+
idx_nxt = end
18+
else: #当前段已经完成
19+
l = end - 1 - start + 1 #计算包括一个异字符的同字符子串长度
20+
if l <= record[text[start]]: #有多的同字符可以把这个夹在中间的同字符换掉
21+
res = max(res, l)
22+
else:
23+
res = max(res, l - 1) #只能用目前字串的边界同字符跟异字符交换
24+
25+
cur = nxt
26+
nxt = None
27+
start, end = idx_nxt, idx_nxt
28+
idx_nxt = 0
29+
if end == len(text) - 1: #到输入终点了
30+
# print end
31+
l = end - start + 1 #计算包括一个异字符的同字符子串长度
32+
# print l
33+
if l <= record[text[start]]: #有多的同字符可以把这个夹在中间的同字符换掉
34+
res = max(res, l)
35+
else:
36+
res = max(res, l - 1) #只能用目前字串的边界同字符跟异字符交换
37+
38+
# print text[start:end + 1]
39+
end += 1
40+
# print end
41+
return res

0 commit comments

Comments
 (0)