Skip to content

Commit 3771c05

Browse files
committed
2019-08-28
1 parent e517d20 commit 3771c05

File tree

5 files changed

+97
-24
lines changed

5 files changed

+97
-24
lines changed

0264.丑数II/0264-丑数II.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def nthUglyNumber(self, n):
3+
"""
4+
:type n: int
5+
:rtype: int
6+
"""
7+
from heapq import *
8+
l = [1]
9+
heapify(l)
10+
cnt = 1
11+
used = set([1])
12+
while cnt < n:
13+
cur = heappop(l)
14+
if cur * 2 not in used:
15+
heappush(l, cur * 2)
16+
used.add(cur * 2)
17+
if cur * 3 not in used:
18+
heappush(l, cur * 3)
19+
used.add(cur * 3)
20+
if cur * 5 not in used:
21+
used.add(cur * 5)
22+
heappush(l, cur * 5)
23+
cnt += 1
24+
return heappop(l)

0265.粉刷房子II/0265-粉刷房子II.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@ def minCostII(self, costs):
44
:type costs: List[List[int]]
55
:rtype: int
66
"""
7-
if not costs or not costs[0]:
7+
if not costs:
88
return 0
9-
dp = costs
109

11-
def GetMin(idx, k):
12-
Min = max(costs[idx])
13-
for i, cost in enumerate(costs[idx]):
14-
if i == k:
15-
continue
16-
Min = min(Min, cost)
17-
return Min
10+
def helper(last, k):
11+
res = max(last)
12+
for i in range(len(last)):
13+
if i != k:
14+
res = min(res, last[i])
15+
return res
1816

1917
for i in range(1, len(costs)):
20-
for k in range(len(costs[i])):
21-
dp[i][k] += GetMin(i - 1, k)
22-
return min(dp[-1])
23-
18+
row = costs[i]
19+
for j in range(len(row)):
20+
row[j] += helper(costs[i - 1], j)
21+
return min(costs[-1])

0266.回文排列/0266-回文排列.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ def canPermutePalindrome(self, s):
44
:type s: str
55
:rtype: bool
66
"""
7-
record = dict()
8-
for i, char in enumerate(s):
9-
record[char] = record.get(char, 0) + 1
7+
flag = 0
8+
dic = collections.Counter(s)
109

11-
odd_cnt = 0
12-
for key, val in record.items():
10+
for key, val in dic.items():
1311
if val % 2:
14-
odd_cnt += 1
15-
if odd_cnt > 1:
12+
if not flag:
13+
flag = 1
14+
else:
1615
return False
16+
1717
return True
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution(object):
2+
def generatePalindromes(self, s):
3+
"""
4+
:type s: str
5+
:rtype: List[str]
6+
"""
7+
if not s or not self.canPermutePalindrome(s):
8+
return []
9+
if len(set(s)) == 1:
10+
return [s]
11+
xor = s[0]
12+
dic = collections.Counter(s)
13+
14+
news = ""
15+
special = ""
16+
for key, val in dic.items():
17+
if val % 2:
18+
special = key
19+
val -= 1
20+
news += key * (val // 2)
21+
# print news
22+
res = set()
23+
def permutations(word, tmp):
24+
if not word:
25+
res.add(tmp + special + tmp[::-1])
26+
27+
for i, char in enumerate(word):
28+
permutations(word[:i] + word[i + 1:], tmp + char)
29+
permutations(news, "")
30+
return list(res)
31+
32+
33+
34+
35+
def canPermutePalindrome(self, s):
36+
"""
37+
:type s: str
38+
:rtype: bool
39+
"""
40+
flag = 0
41+
dic = collections.Counter(s)
42+
43+
for key, val in dic.items():
44+
if val % 2:
45+
if not flag:
46+
flag = 1
47+
else:
48+
return False
49+
50+
return True

0268.缺失数字/0268-缺失数字.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ def missingNumber(self, nums):
44
:type nums: List[int]
55
:rtype: int
66
"""
7-
l = len(nums)
8-
idealsum = l*(l+1) /2
9-
realsum = sum(nums)
10-
return idealsum - realsum
7+
s = set(nums)
8+
for num in range(len(nums)):
9+
if num not in s:
10+
return num
11+
return len(nums)

0 commit comments

Comments
 (0)