If JavaScript exceeds the length of the array, no error will be reported
Today, I encountered such a problem when I was doing the problem
while (sum < target) {
right++;
sum += nums[right];
}
Here, the while loop does not add the limit when the right index exceeds the length of the array, but there is still no error. The program can run normally.
The following reasons are checked here. It is found that when right exceeds the range, the output of num [right] is undefined
console.log(nums[nums.length]) //undefined
When undefined is added to the number, it becomes Nan
console.log(10+nums[nums.length]) // NaN
In the judgment conditions of the while loop, comparing Nan with the number will directly return false, so you can exit the loop without affecting the result.
console.log(NaN>10) //false
Title: 209. Minimum length subarray
https://leetcode-cn.com/problems/minimum-size-subarray-sum/
When trying to abbreviate it as suffix increment, I forgot to change the initial value of right from – 1 to 0. JavaScript cannot traverse the array through the negative index and will directly return undefined. Therefore, combined with the above example, it will directly exit the first small while loop. Right remains unchanged and will fall into an endless loop.
Error demonstration:
var minSubArrayLen = function(target, nums) {
let left = 0, right = -1;
let sum = 0;
let res = nums.length+1;
while (right<nums.length) {
while (sum < target) {
// right++;
sum += nums[++right];
}
while (sum>=target) {
res = (res>right-left+1)?right-left+1 : res;
sum -= nums[left];
left ++;
}
}
return res>nums.length ?0 : res;
}
Read More:
- JavaScript removes the number specified in the array
- Array of PHP_ diff,array_ intersect,array_ merge, in_ Is there a limit on the number of arrays in array?
- [Solved] RSA decrypt error: the data to be decrypted exceeds the maximum 128 bytes of this module
- In R language, for loop or array truncation, the following error occurs only 0’s may be mixed with negative subscripts
- How to uninstall the software for apple / Mac? What if the software can’t be unloaded?
- Lingerror last 2 dimensions of the array must be square
- Leetcode 34 finds the first and last position of an element in the sorted array (medium)
- [solved] error: valueerror: expected 2D array, got scalar array instead
- Vs2017 reported an error. Pthread. H header file cannot be opened and cannot be found
- Hzero – if the local swagger fails to register, the connection timeout or gateway error will be displayed
- Leetcode solution 189 Rotate Array Java version
- error C2057: expected constant expression (Can the size of an array in C language be defined when the program is running?)
- Type error: the JSON object must be STR, bytes or byte array, not ‘textiowrapper’
- If you open the store in iTunes, you will be prompted to solve the 310 error
- 【ipfs-api】npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.
- Two dimensional array and pointer to one dimensional array
- Error using ones Size inputs must be integers. Error in testdisplay1 (line 38) array=-ones(buf+m*(s
- vector length_error
- JavaScript realizes the longest substring of non repeated characters
- Implement Set using Array.