/**
* @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 发布
本文提供了一种寻找数组中缺失数字的方法,通过遍历数组并利用数组下标与元素之间的关系进行计算,实现时间复杂度为O(n)且空间复杂度为O(1)的解决方案。
337

被折叠的 条评论
为什么被折叠?



