From 3771c05d2690306c3298b7d70eebfd57c758e5ce Mon Sep 17 00:00:00 2001 From: Celia_Wu <530081999@qq.com> Date: Wed, 28 Aug 2019 19:36:00 -0400 Subject: [PATCH] 2019-08-28 --- .../0264-\344\270\221\346\225\260II.py" | 24 +++++++++ ...\345\210\267\346\210\277\345\255\220II.py" | 24 ++++----- ...36\346\226\207\346\216\222\345\210\227.py" | 14 +++--- ...\346\226\207\346\216\222\345\210\227II.py" | 50 +++++++++++++++++++ ...72\345\244\261\346\225\260\345\255\227.py" | 9 ++-- 5 files changed, 97 insertions(+), 24 deletions(-) create mode 100644 "0264.\344\270\221\346\225\260II/0264-\344\270\221\346\225\260II.py" create mode 100644 "0267.\345\233\236\346\226\207\346\216\222\345\210\227II/0267-\345\233\236\346\226\207\346\216\222\345\210\227II.py" diff --git "a/0264.\344\270\221\346\225\260II/0264-\344\270\221\346\225\260II.py" "b/0264.\344\270\221\346\225\260II/0264-\344\270\221\346\225\260II.py" new file mode 100644 index 0000000..533b3c1 --- /dev/null +++ "b/0264.\344\270\221\346\225\260II/0264-\344\270\221\346\225\260II.py" @@ -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) \ No newline at end of file diff --git "a/0265.\347\262\211\345\210\267\346\210\277\345\255\220II/0265-\347\262\211\345\210\267\346\210\277\345\255\220II.py" "b/0265.\347\262\211\345\210\267\346\210\277\345\255\220II/0265-\347\262\211\345\210\267\346\210\277\345\255\220II.py" index 61bbf42..1d59202 100644 --- "a/0265.\347\262\211\345\210\267\346\210\277\345\255\220II/0265-\347\262\211\345\210\267\346\210\277\345\255\220II.py" +++ "b/0265.\347\262\211\345\210\267\346\210\277\345\255\220II/0265-\347\262\211\345\210\267\346\210\277\345\255\220II.py" @@ -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]) - \ No newline at end of file + row = costs[i] + for j in range(len(row)): + row[j] += helper(costs[i - 1], j) + return min(costs[-1]) \ No newline at end of file diff --git "a/0266.\345\233\236\346\226\207\346\216\222\345\210\227/0266-\345\233\236\346\226\207\346\216\222\345\210\227.py" "b/0266.\345\233\236\346\226\207\346\216\222\345\210\227/0266-\345\233\236\346\226\207\346\216\222\345\210\227.py" index 2d62de5..76e1ea3 100644 --- "a/0266.\345\233\236\346\226\207\346\216\222\345\210\227/0266-\345\233\236\346\226\207\346\216\222\345\210\227.py" +++ "b/0266.\345\233\236\346\226\207\346\216\222\345\210\227/0266-\345\233\236\346\226\207\346\216\222\345\210\227.py" @@ -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 \ No newline at end of file diff --git "a/0267.\345\233\236\346\226\207\346\216\222\345\210\227II/0267-\345\233\236\346\226\207\346\216\222\345\210\227II.py" "b/0267.\345\233\236\346\226\207\346\216\222\345\210\227II/0267-\345\233\236\346\226\207\346\216\222\345\210\227II.py" new file mode 100644 index 0000000..4021cea --- /dev/null +++ "b/0267.\345\233\236\346\226\207\346\216\222\345\210\227II/0267-\345\233\236\346\226\207\346\216\222\345\210\227II.py" @@ -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 \ No newline at end of file diff --git "a/0268.\347\274\272\345\244\261\346\225\260\345\255\227/0268-\347\274\272\345\244\261\346\225\260\345\255\227.py" "b/0268.\347\274\272\345\244\261\346\225\260\345\255\227/0268-\347\274\272\345\244\261\346\225\260\345\255\227.py" index 0f53d04..4184db1 100644 --- "a/0268.\347\274\272\345\244\261\346\225\260\345\255\227/0268-\347\274\272\345\244\261\346\225\260\345\255\227.py" +++ "b/0268.\347\274\272\345\244\261\346\225\260\345\255\227/0268-\347\274\272\345\244\261\346\225\260\345\255\227.py" @@ -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 \ No newline at end of file + s = set(nums) + for num in range(len(nums)): + if num not in s: + return num + return len(nums) \ No newline at end of file