leetcode 268. Missing Number
2017-07-28 16:03
417 查看
Given an array containing n distinct numbers taken from
For example,
Given nums =
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
呵呵
0, 1, 2, ..., n, find the one that is missing from the array.
For example,
Given nums =
[0, 1, 3]return
2.
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
呵呵
class Solution { public: int missingNumber(vector<int>& nums) { int sum = 0; for (int x : nums) { sum += x; } int n = nums.size(); return n * (1 + n) / 2 - sum; } };
相关文章推荐
- 【LeetCode】 268. Missing Number
- leetcode - 268. Missing Number
- LeetCode 268. Missing Number - 求和、异或
- LeetCode笔记:268. Missing Number
- [leetcode]-268. Missing Number(C语言)
- 2018.03.15 leetcode 打卡 #268. Missing Number
- [LeetCode]268. Missing Number
- 268. Missing Number(leetcode O(n))
- 2018.03.15 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
- 2018.03.15 leetcode 打卡 #268. Missing Number