File tree 4 files changed +93
-0
lines changed
014.Longest Common Prefix
4 files changed +93
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def longestCommonPrefix (self , strs ):
3
+ """
4
+ :type strs: List[str]
5
+ :rtype: str
6
+ """
7
+ if strs == []:
8
+ return ''
9
+ if len (strs ) == 1 :
10
+ return strs [0 ]
11
+ strs .sort (key = lambda x : len (x ))
12
+ flag = False
13
+ prefix = ''
14
+ for j in range (1 ,len (strs [0 ])+ 1 ):
15
+ prefix = strs [0 ][:j ]
16
+ for i in range (1 ,len (strs )):
17
+ if prefix == strs [i ][:j ]:
18
+ flag = True
19
+ else :
20
+ flag = False
21
+ if not flag :
22
+ prefix0 = prefix [:- 1 ]
23
+ return prefix0
24
+ break
25
+ return prefix
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def permute (self , nums ):
3
+ """
4
+ :type nums: List[int]
5
+ :rtype: List[List[int]]
6
+ """
7
+ if len (nums ) <= 1 :
8
+ return [nums ]
9
+ ans = []
10
+ for i , num in enumerate (nums ):
11
+ n = nums [:i ] + nums [i + 1 :]
12
+ for y in self .permute (n ):
13
+ ans .append ([num ] + y )
14
+ return ans
15
+
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def permuteUnique (self , nums ):
3
+ ans = [[]]
4
+ for n in nums :
5
+ new_ans = []
6
+ for l in ans :
7
+ for i in range (len (l )+ 1 ):
8
+ new_ans .append (l [:i ]+ [n ]+ l [i :])
9
+ if i < len (l ) and l [i ]== n : break #handles duplication
10
+ ans = new_ans
11
+ return ans
12
+
13
+ class Solution :
14
+ def permute (self , nums ):
15
+ """
16
+ :type nums: List[int]
17
+ :rtype: List[List[int]]
18
+ """
19
+ if len (nums ) <= 1 :
20
+ return [nums ]
21
+ ans = []
22
+ for i , num in enumerate (nums ):
23
+ n = nums [:i ] + nums [i + 1 :]
24
+ for y in self .permute (n ):
25
+ ans .append ([num ] + y )
26
+ return ans
27
+
28
+ def permuteUnique (self , nums ):
29
+ """
30
+ :type nums: List[int]
31
+ :rtype: List[List[int]]
32
+ """
33
+ ans = self .permute (nums )
34
+ temp = []
35
+ for i in ans :
36
+ if i in temp :
37
+ pass
38
+ else :
39
+ temp .append (i )
40
+ return temp
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isPowerOfTwo (self , n ):
3
+ """
4
+ :type n: int
5
+ :rtype: bool
6
+ """
7
+ if n <= 0 :
8
+ return False
9
+ m = 2 ** 32
10
+ if m % n :
11
+ return False
12
+ else :
13
+ return True
You can’t perform that action at this time.
0 commit comments