您现在的位置是:主页 > news > 做搜狗pc网站点/郑州关键词排名公司电话
做搜狗pc网站点/郑州关键词排名公司电话
admin2025/5/22 2:04:40【news】
简介做搜狗pc网站点,郑州关键词排名公司电话,网络工程属于什么大类,网站备案 关闭题目: Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array. 思路: 不断地把每个元素换到它应该在的位置上,然后看看哪个位置上的元素不对,则缺少的就是那一位…
做搜狗pc网站点,郑州关键词排名公司电话,网络工程属于什么大类,网站备案 关闭题目: Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array. 思路: 不断地把每个元素换到它应该在的位置上,然后看看哪个位置上的元素不对,则缺少的就是那一位…
题目:
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.
思路:
不断地把每个元素换到它应该在的位置上,然后看看哪个位置上的元素不对,则缺少的就是那一位元素。
其他思路可以看下博文https://blog.csdn.net/gao1440156051/article/details/51899380,很巧妙。
代码实现:
submit一次就过了。
class Solution {
public:void swap(int &a, int &b){int t = a;a = b;b = t;}int missingNumber(vector<int>& nums) {for (int i = 0; i < nums.size(); ++i){if (nums[i] >= nums.size() || nums[i] == i){continue;}while (nums[i] != i && nums[i] < nums.size()){swap(nums[i], nums[nums[i]]);}}int ans;for (int i = 0; i < nums.size(); ++i){if (nums[i] != i){ans = i;break;}}return ans;}
};
参考:
https://blog.csdn.net/gao1440156051/article/details/51899380