File tree Expand file tree Collapse file tree 2 files changed +66
-1
lines changed
Expand file tree Collapse file tree 2 files changed +66
-1
lines changed Original file line number Diff line number Diff line change 11'''
22# 21. Merge Two Sorted Lists
33
4- A. iterative approach: use a temp node to store the result .
4+ A. iterative approach: use a two pointers to merge the two lists .
55B. recursive approach: use recursion to merge the two lists.
66
77
Original file line number Diff line number Diff line change 1+ '''
2+ # 268. Missing Number
3+
4+ A. iterative approach: sort the array and find the missing number.
5+ B. XOR approach: use XOR to find the missing number.
6+ - a ^ a = 0, a ^ 0 = a
7+
8+ ## Time and Space Complexity
9+
10+ ### A. Iterative Approach
11+
12+ ```
13+ TC: O(n)
14+ SC: O(1)
15+ ```
16+
17+ #### TC is O(n):
18+ - sorting the array. = O(n log n)
19+ - iterating through the array just once to find the missing number. = O(n)
20+
21+ #### SC is O(1):
22+ - no extra space is used. = O(1)
23+
24+ ### B. XOR Approach
25+
26+ ```
27+ TC: O(n)
28+ SC: O(1)
29+ ```
30+
31+ #### TC is O(n):
32+ - iterating through the array just once to find the missing number. = O(n)
33+
34+ #### SC is O(1):
35+ - no extra space is used. = O(1)
36+
37+ '''
38+ class Solution :
39+ '''
40+ A. Iterative Approach
41+ '''
42+ def missingNumberIterative (self , nums : List [int ]) -> int :
43+ nums .sort ()
44+ n = len (nums )
45+
46+ for i in range (n ):
47+ if nums [i ] != i :
48+ return i
49+ return n
50+
51+ '''
52+ B. XOR Approach
53+ '''
54+ def missingNumberXOR (self , nums : List [int ]) -> int :
55+ n = len (nums )
56+ xor_all = 0
57+ xor_nums = 0
58+
59+ for i in range (n + 1 ):
60+ xor_all ^= i
61+
62+ for num in nums :
63+ xor_nums ^= num
64+
65+ return xor_all ^ xor_nums
You can’t perform that action at this time.
0 commit comments