/**
* @author johnsondu
* @time 2015.8.26 23:35
* @problem Missing Number(from leetcode)
* @timeComplexity O(n)
* @spaceComplexity O(1)
* @strategy from index 0 to numofSize(nums),
* plus i and subtract the number in the vector
*
*/
class Solution {
public:
int missingNumber(vector<int>& nums) {
int cnt = 0;
int len = nums.size();
if(len == 0) return 0;
for(int i = 0; i < len; i ++)
cnt += i - nums[i];
cnt += len;
return cnt;
}
};
【leetcode】268. Missing Number
最新推荐文章于 2023-10-11 12:12:13 发布