Комментарии:
If there are any videos you'd like me to make or if you have any ideas on how to optimize this solution, let me know!
ОтветитьYour explanation is on another level entirely... very detailed..well done 👍
Ответитьbro u can also do with xor of [0, 1, 3] ^ [0, 1 , 2, 3]
ОтветитьHello, this is my alternative solution. Thought I'd share.
const missingNum = (nums) => {
nums = [...new Set(nums)].sort((a,b) => a-b);
const lengthWithMissingNum = nums.length + 1;
for (let i = 0; i <= lengthWithMissingNum; i++) {
if (nums[i] !== i) {
return i;
}
}
}
var missingNumber = function(nums) {
let numsLen =nums.length
let missingSum =0
let actualSum =numsLen*(numsLen+1)/2
for(let i=0;i<numsLen;i++){
missingSum+=nums[i]
}
return actualSum-missingSum
};
how does one figure this trick out with the indices? that difference of the sums of the array, gives this correct output? I think in an interview setting i would only be able to do the solution on leetcode which uses extra space
ОтветитьYet another very beautiful explanation
Ответить