File tree Expand file tree Collapse file tree 5 files changed +161
-0
lines changed Expand file tree Collapse file tree 5 files changed +161
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ 338. Counting Bits
3+ https://leetcode.com/problems/counting-bits/description/
4+
5+ Solution:
6+ - Convert the number to binary string
7+ - Sum the number of 1s in the binary string
8+ - Append the sum to the output list
9+ - Return the output list
10+
11+ Time complexity: O(nlogn)
12+ - The for loop runs n times
13+ - The sum function runs O(logn) times
14+
15+ Space complexity: O(n)
16+ - The output list has n elements
17+ """
18+
19+
20+ from typing import List
21+
22+ class Solution :
23+ def countBits (self , n : int ) -> List [int ]:
24+
25+ output = []
26+ for i in range (n + 1 ):
27+ _str = str (bin (i ))[2 :]
28+ _sum = sum (map (int , _str .strip ()))
29+ output .append (_sum )
30+
31+ return output
Original file line number Diff line number Diff line change 1+ """
2+ 49. Group Anagrams
3+ https://leetcode.com/problems/group-anagrams/description/
4+
5+ Solution:
6+ - Create a hash table and a list of counters
7+ - For each string in the input list:
8+ - Sort the string
9+ - If the sorted string is in the hash table:
10+ - Append the string to the corresponding counter list
11+ - Else:
12+ - Add the sorted string to the hash table
13+ - Create a new counter list and append the string
14+ - Return the list of counters
15+
16+ Time complexity: O(nmlogm)
17+ - The for loop runs n times
18+ - The sorted function runs O(mlogm) times
19+ - m is the length of the longest string in the input list
20+
21+ Space complexity: O(n)
22+ - The output list has n elements
23+ - The hash table has n elements
24+ - The list of counters has n elements
25+ """
26+
27+ from typing import List
28+
29+ class Solution :
30+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
31+
32+ hash_table = []
33+ counters = []
34+ for s in strs :
35+
36+ count_s = '' .join (sorted (s ))
37+ if count_s in hash_table :
38+ idx = hash_table .index (count_s )
39+ counters [idx ].append (s )
40+ else :
41+ hash_table .append (count_s )
42+ counters .append ([s ])
43+
44+ return counters
Original file line number Diff line number Diff line change 1+ """
2+ 268. Missing Number
3+ https://leetcode.com/problems/missing-number/description/
4+
5+ Solution:
6+ - Sort the input list
7+ - For each index in the input list:
8+ - If the index is not equal to the element:
9+ - Return the index
10+ - Return the length of the input list
11+
12+ Time complexity: O(nlogn+n)
13+ - The sort function runs O(nlogn) times
14+ - The for loop runs n times
15+
16+ Space complexity: O(1)
17+ - No extra space is used
18+ """
19+ from typing import List
20+
21+ class Solution :
22+ def missingNumber (self , nums : List [int ]) -> int :
23+
24+ nums .sort ()
25+ for i in range (len (nums )):
26+ if i != nums [i ]:
27+ return i
28+
29+ return len (nums )
30+
Original file line number Diff line number Diff line change 1+ """
2+ 191. Number of 1 Bits
3+ https://leetcode.com/problems/number-of-1-bits/
4+
5+ Solution:
6+ - Create a counter
7+ - For each bit in the input number:
8+ - If the bit is 1:
9+ - Increment the counter
10+ - Return the counter
11+
12+ Time complexity: O(1)
13+ - The while loop runs 32 times
14+
15+ Space complexity: O(1)
16+ - No extra space is used
17+ """
18+
19+ class Solution :
20+ def hammingWeight (self , n : int ) -> int :
21+ output = 0
22+
23+ i = 1 << 31
24+ while (i > 0 ) :
25+
26+ if ((n & i ) != 0 ) :
27+ output += 1
28+
29+ i = i // 2
30+
31+ return output
Original file line number Diff line number Diff line change 1+ """
2+ 190. Reverse Bits
3+ https://leetcode.com/problems/reverse-bits/description/
4+
5+ Solution:
6+ - Convert the number to binary string
7+ - Reverse the binary string
8+ - Convert the reversed binary string to integer
9+ - Return the integer
10+
11+ Time complexity: O(1)
12+ - The bin function runs once
13+ - The reversed function runs once
14+ - The int function runs once
15+
16+ Space complexity: O(1)
17+ - No extra space is used
18+
19+ """
20+
21+ class Solution :
22+ def reverseBits (self , n : int ) -> int :
23+ n_bin = bin (n )[2 :].zfill (32 )
24+ n_bin = '' .join (reversed (n_bin ))
25+ return int (n_bin , base = 2 )
You can’t perform that action at this time.
0 commit comments