https://leetcode.com/problems/missing-number/description/
Solution 1.
int missingNumber(vector<int>& nums) {
int res = 0;
for(int i=0; i<nums.size(); i++) {
res ^= (i ^ nums[i]);
}
return res^nums.size();
}
Solution 2.
int missingNumber(vector<int>& nums) {
vector<int> s(nums.size()+1, -1);
for(int i=0; i<nums.size(); i++) {
s[nums[i]] = 1;
}
for(int i=0; i<s.size(); i++) {
if(s[i] == -1) return i;
}
return -1;
}
No comments:
Post a Comment