Skip to content

Instantly share code, notes, and snippets.

@iBaozi
Created September 19, 2015 06:36
Show Gist options
  • Save iBaozi/14a04bedf52d46ec994a to your computer and use it in GitHub Desktop.
Save iBaozi/14a04bedf52d46ec994a to your computer and use it in GitHub Desktop.
/**
* @param {number[]} nums
* @return {number}
*/
function find(nums, v) {
for (var j in nums) {
if (v == nums[j]) {
return true
}
}
return false;
}
var missingNumber = function(nums) {
// sort
/*
var max = nums.length, i = 0;
nums.sort(function(a,b){
return a-b;
});
while(i<max) {
if (nums[i] != i) {
return i;
}
i++;
}
return nums.pop() + 1;
*/
// o(n*n)
/*
var max = nums.length, i = 0;
while(i<=max) {
if (!find(nums, i)) {
return i;
}
i++;
}
*/
// o(n)
var len = nums.length;
if (len == 1) {
return 1 - nums[0];
}
sum = (1+len) * (len) / 2;
for (var i in nums) {
sum -= nums[i];
}
return sum;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment