Skip to content

Commit

Permalink
2019-08-28
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Aug 28, 2019
1 parent e517d20 commit 3771c05
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 24 deletions.
24 changes: 24 additions & 0 deletions 0264.丑数II/0264-丑数II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution(object):
def nthUglyNumber(self, n):
"""
:type n: int
:rtype: int
"""
from heapq import *
l = [1]
heapify(l)
cnt = 1
used = set([1])
while cnt < n:
cur = heappop(l)
if cur * 2 not in used:
heappush(l, cur * 2)
used.add(cur * 2)
if cur * 3 not in used:
heappush(l, cur * 3)
used.add(cur * 3)
if cur * 5 not in used:
used.add(cur * 5)
heappush(l, cur * 5)
cnt += 1
return heappop(l)
24 changes: 11 additions & 13 deletions 0265.粉刷房子II/0265-粉刷房子II.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ def minCostII(self, costs):
:type costs: List[List[int]]
:rtype: int
"""
if not costs or not costs[0]:
if not costs:
return 0
dp = costs

def GetMin(idx, k):
Min = max(costs[idx])
for i, cost in enumerate(costs[idx]):
if i == k:
continue
Min = min(Min, cost)
return Min
def helper(last, k):
res = max(last)
for i in range(len(last)):
if i != k:
res = min(res, last[i])
return res

for i in range(1, len(costs)):
for k in range(len(costs[i])):
dp[i][k] += GetMin(i - 1, k)
return min(dp[-1])
row = costs[i]
for j in range(len(row)):
row[j] += helper(costs[i - 1], j)
return min(costs[-1])
14 changes: 7 additions & 7 deletions 0266.回文排列/0266-回文排列.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ def canPermutePalindrome(self, s):
:type s: str
:rtype: bool
"""
record = dict()
for i, char in enumerate(s):
record[char] = record.get(char, 0) + 1
flag = 0
dic = collections.Counter(s)

odd_cnt = 0
for key, val in record.items():
for key, val in dic.items():
if val % 2:
odd_cnt += 1
if odd_cnt > 1:
if not flag:
flag = 1
else:
return False

return True
50 changes: 50 additions & 0 deletions 0267.回文排列II/0267-回文排列II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Solution(object):
def generatePalindromes(self, s):
"""
:type s: str
:rtype: List[str]
"""
if not s or not self.canPermutePalindrome(s):
return []
if len(set(s)) == 1:
return [s]
xor = s[0]
dic = collections.Counter(s)

news = ""
special = ""
for key, val in dic.items():
if val % 2:
special = key
val -= 1
news += key * (val // 2)
# print news
res = set()
def permutations(word, tmp):
if not word:
res.add(tmp + special + tmp[::-1])

for i, char in enumerate(word):
permutations(word[:i] + word[i + 1:], tmp + char)
permutations(news, "")
return list(res)




def canPermutePalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
flag = 0
dic = collections.Counter(s)

for key, val in dic.items():
if val % 2:
if not flag:
flag = 1
else:
return False

return True
9 changes: 5 additions & 4 deletions 0268.缺失数字/0268-缺失数字.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ def missingNumber(self, nums):
:type nums: List[int]
:rtype: int
"""
l = len(nums)
idealsum = l*(l+1) /2
realsum = sum(nums)
return idealsum - realsum
s = set(nums)
for num in range(len(nums)):
if num not in s:
return num
return len(nums)

0 comments on commit 3771c05

Please sign in to comment.