Skip to content

Commit fe40360

Browse files
committed
update solution: missing-number
1 parent 659da78 commit fe40360

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

missing-number/dusunax.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
44
A. iterative approach: sort the array and find the missing number.
55
B. XOR approach: use XOR to find the missing number.
6-
- a ^ a = 0, a ^ 0 = a
6+
- a ^ a = 0, a ^ 0 = a
77
88
## Time and Space Complexity
99
1010
### A. Iterative Approach
1111
1212
```
13-
TC: O(n)
13+
TC: O(n log n)
1414
SC: O(1)
1515
```
1616
@@ -53,13 +53,11 @@ def missingNumberIterative(self, nums: List[int]) -> int:
5353
'''
5454
def missingNumberXOR(self, nums: List[int]) -> int:
5555
n = len(nums)
56-
xor_all = 0
5756
xor_nums = 0
5857

5958
for i in range(n + 1):
60-
xor_all ^= i
61-
62-
for num in nums:
63-
xor_nums ^= num
59+
if i < n:
60+
xor_nums ^= nums[i]
61+
xor_nums ^= i
6462

65-
return xor_all^xor_nums
63+
return xor_nums

0 commit comments

Comments
 (0)