简介
使用哈希表来表述.
code
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
map<int, bool> m;
for(auto it : nums){
m[it] = true;
}
for(int i=0; i<=n; i++){
if(m[i] == false){
return i;
}
}
return -1;
}
};
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
Map<Integer, Boolean> m = new HashMap<Integer, Boolean>(); // 要使用 boolean 的封装类 以及 int 的封装类
for(int i=0; i<n; i++){
m.put(nums[i], true);
}
for(int i=0; i<=n; i++){
if(!m.containsKey(i)){ // 不能直接使用get会报错.
return i;
}
}
return -1;
}
}
---------------------------我的天空里没有太阳,总是黑夜,但并不暗,因为有东西代替了太阳。虽然没有太阳那么明亮,但对我来说已经足够。凭借着这份光,我便能把黑夜当成白天。我从来就没有太阳,所以不怕失去。
--------《白夜行》
浙公网安备 33010602011771号