-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
97 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters