File tree Expand file tree Collapse file tree 5 files changed +97
-24
lines changed Expand file tree Collapse file tree 5 files changed +97
-24
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change @@ -4,20 +4,18 @@ def minCostII(self, costs):
4
4
:type costs: List[List[int]]
5
5
:rtype: int
6
6
"""
7
- if not costs or not costs [ 0 ] :
7
+ if not costs :
8
8
return 0
9
- dp = costs
10
9
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
18
16
19
17
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 ])
Original file line number Diff line number Diff line change @@ -4,14 +4,14 @@ def canPermutePalindrome(self, s):
4
4
:type s: str
5
5
:rtype: bool
6
6
"""
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 )
10
9
11
- odd_cnt = 0
12
- for key , val in record .items ():
10
+ for key , val in dic .items ():
13
11
if val % 2 :
14
- odd_cnt += 1
15
- if odd_cnt > 1 :
12
+ if not flag :
13
+ flag = 1
14
+ else :
16
15
return False
16
+
17
17
return True
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -4,7 +4,8 @@ def missingNumber(self, nums):
4
4
:type nums: List[int]
5
5
:rtype: int
6
6
"""
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 )
You can’t perform that action at this time.
0 commit comments