leetcode 268. Missing Number
2017-05-27 17:33
309 查看
Given an array containing n distinct numbers taken from
find the one that is missing from the array.
For example,
Given nums =
涉及到位操作:异或
code:
int missingNumber(vector<int>& nuts)
{
int res=0;
for (int i=0;i<nums.size();i++){
res^=nums[i]^i;
}
res^=nums.size();
return res;
}math 方法
int missingNumber(vector<int>& nums) {
int n=nums.size();
int total = n*(n+1)/2;
for (auto &x:nums){
total-=x;
}
return total;
}
0, 1, 2, ..., n,
find the one that is missing from the array.
For example,
Given nums =
[0, 1, 3]return
2.
涉及到位操作:异或
code:
int missingNumber(vector<int>& nuts)
{
int res=0;
for (int i=0;i<nums.size();i++){
res^=nums[i]^i;
}
res^=nums.size();
return res;
}math 方法
int missingNumber(vector<int>& nums) {
int n=nums.size();
int total = n*(n+1)/2;
for (auto &x:nums){
total-=x;
}
return total;
}
相关文章推荐
- Leetcode-268. Missing Number
- LeetCode 268. Missing Number
- LeetCode 268. Missing Number
- leetcode268. Missing Number
- 【LeetCode】268. Missing Number
- LeetCode 268. Missing Number
- 2018.03.15 leetcode 打卡 #268. Missing Number
- [leetcode] 268. Missing Number
- LeetCode: 268. Missing Number
- Leetcode 268. Missing Number
- Leetcode-268. Missing Number
- LeetCode268. Missing Number
- [Leetcode] 268. Missing Number
- 2018.03.15 leetcode 打卡 #268. Missing Number
- leetcode:268. Missing Number(Java)解答
- 【leetcode】268. Missing Number
- Leetcode 268. Missing Number (Medium) (cpp)
- leetcode-268. Missing Number
- [LeetCode] 268. Missing Number
- 2018.03.15 leetcode 打卡 #268. Missing Number