Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
1 ^ 1 = 0
0 ^ 0 = 0
0 ^ 1 = 1
XOR์ ์ด์ฒ๋ผ ์๋ก ๊ฐ์ผ๋ฉด 0, ๋ค๋ฅด๋ฉด 1์ ๋ถ์ฌํ๋ ๋นํธ์ฐ์ฐ์ด๋ค.
์ค์ํ ํน์ฑ์, 0๊ณผ a๊ฐ XORํ๋ฉด ๋ฌด์กฐ๊ฑด a๊ฐ ๋์ค๊ฒ ๋์ด์๋ค.
์ฆ, ๋ฒ์ [0...n] ์ ๋ชจ๋ ์์์ XOR์ ํ๋ฉด, ์๋ก ๋ค๋ฅธ ์ ์ผ ๋ ๋ฌด์กฐ๊ฑด 0์ด ์๋ ์๊ฐ ๋์จ๋ค! -> ์ฌ๋ผ์ง ์๊ฐ ๋๋ค.
class Solution:
def missingNumber(self, nums: List[int]) -> int:
missing = len(nums)
for i,num in enumerate(nums):
missing ^= i ^ num
return missing