Solution Code
C#
public class Solution {
public int MissingNumber(int[] nums) {
int n = nums.Length;
int missing = n;
for (int i = 0; i < n; i++)
missing ^= i ^ nums[i];
return missing;
}
}
C++
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
int missing = n;
for (int i = 0; i < n; i++)
missing ^= i ^ nums[i];
return missing;
}
};
Java
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int missing = n;
for (int i = 0; i < n; i++)
missing ^= i ^ nums[i];
return missing;
}
}
Python
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
missing = n
for i in range(0, n):
missing ^= i ^ nums[i]
return missing
Ruby
# @param {Integer[]} nums
# @return {Integer}
def missing_number(nums)
n = nums.length()
missing = n
n.times { |i| missing ^= i ^ nums[i] }
missing
end
Resources