448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements of [1, n] inclusive that do not appear in this array.
Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
/**
* @param {number[]} nums
* @return {number[]}
*/
var findDisappearedNumbers = function(nums) {
var n = nums.length;
var s = nums.toString().replace(/,/g,"");
var arr = [];
//var t = 0;
for(let i=1; i<n+1; i++){
if(s.indexOf(i)<0 ){
let j = 0 ;
j = arr.push(i);
//t+=i;
}else{
continue;
}
}
return (arr);
};
第一版: for 裡面 i = 0 檢查會查到0
第二版: 用 tostring 假如 有10 卻沒出現1 用1檢查時仍會出現XD
先SORt 將 >9的 非個位數 依序切出來
剩下的去跑檢查
切出來的 自己在跑一次
留言
張貼留言