File tree 4 files changed +56
-0
lines changed
4 files changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+
4
+ class Solution :
5
+ def containsDuplicate (self , nums : List [int ]) -> bool :
6
+ return len (nums ) != len (set (nums ))
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def hammingWeight (self , n : int , acc : int = 0 ) -> int :
3
+ if n == 0 :
4
+ return acc
5
+
6
+ return self .hammingWeight (n // 2 , acc + n % 2 )
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countPalindrome (self , s : str , left : int , right : int ) -> int :
3
+ result = 0
4
+
5
+ while left >= 0 and right < len (s ) and s [left ] == s [right ]:
6
+ result += 1
7
+ left -= 1
8
+ right += 1
9
+
10
+ return result
11
+
12
+ def countSubstrings (self , s : str ) -> int :
13
+ total_count = 0
14
+
15
+ for i in range (len (s )):
16
+ left = right = i
17
+ total_count += self .countPalindrome (s , left , right )
18
+
19
+ for i in range (len (s ) - 1 ):
20
+ left , right = i , i + 1
21
+ total_count += self .countPalindrome (s , left , right )
22
+
23
+ return total_count
Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+
4
+ class Solution :
5
+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
6
+ count_dict = {}
7
+ frequency_bucket = [[] for i in range (len (nums ) + 1 )]
8
+ result = []
9
+
10
+ for num in nums :
11
+ count_dict [num ] = count_dict .get (num , 0 ) + 1
12
+
13
+ for num , count in count_dict .items ():
14
+ frequency_bucket [count ].append (num )
15
+
16
+ for i in range (len (frequency_bucket ) - 1 , 0 , - 1 ):
17
+ for num in frequency_bucket [i ]:
18
+ result .append (num )
19
+
20
+ if len (result ) == k :
21
+ return result
You can’t perform that action at this time.
0 commit comments