-
开辟一个足够大的数组arr
-
遍历nums,如果nums[i] > 0 就 arr[i]++
-
最后遍历arr返回第一个arr[i] == 0 的位置
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型vector
* @return int整型
*/
int minNumberDisappeared(vector<int>& nums) {
// write code here
int arr[500001] = {0};
for (vector<int>::iterator it = nums.begin(); it != nums.end(); it++) {
if (*it > 0) arr[*it]++;
}
int pos = 1;
while (pos > 0) {
if (arr[pos] == 0) return pos;
++pos;
}
return -1;
}
};
static const auto io_sync_off = []()//lambda函数
{
// turn off sync,关闭输入输出流的缓存
std::ios::sync_with_stdio(false);
// untie in/out streams,实现输入和输出流的解绑
std::cin.tie(nullptr);
return nullptr;
}();