We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 659da78 commit fe40360Copy full SHA for fe40360
missing-number/dusunax.py
@@ -3,14 +3,14 @@
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
+ - a ^ a = 0, a ^ 0 = a
7
8
## Time and Space Complexity
9
10
### A. Iterative Approach
11
12
```
13
-TC: O(n)
+TC: O(n log n)
14
SC: O(1)
15
16
@@ -53,13 +53,11 @@ def missingNumberIterative(self, nums: List[int]) -> int:
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
+ if i < n:
+ xor_nums ^= nums[i]
+ xor_nums ^= i
64
65
- return xor_all^xor_nums
+ return xor_nums
0 commit comments