Created
September 19, 2015 06:36
-
-
Save iBaozi/14a04bedf52d46ec994a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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