In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to solve the jump game algorithm problem with JavaScript". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "JavaScript how to solve the jump game algorithm problem" bar!
Topic: given an array of non-negative integers, you are initially in the first position of the array. Each element in the array represents the maximum length you can jump at that location. Judge whether you can reach the last position.
Example 1:
Input: [2, 3, 1, 1, 1, 4]
Output: true
Explanation: jump 1 step from position 0 to 1, then jump 3 steps to the last position.
Example 2:
Input: [3, 2, 1, 0, 4]
Output: false
Explanation: in any case, you will always reach the position with an index of 3. But the maximum jump length for this position is 0
So you'll never get to the last position.
Through observation, it is found that:
If there is no 0 in the array, you can definitely skip to the end. If there is a 0 in the array, the following conditions must be met in order to jump to the end, and the 0 must be skipped from a position before 0.
For example, the array in example 2 can skip to the end if it is:
1 、 [4,2,1,0,4]
2 、 [3,3,1,0,4]
3 、 [3,2,2,0,4]
What pattern did you find? The value of the player's position must be greater than the distance between the 0 location index and the current location index.
4 > 3-0, the index value of position 4 is 0, and the difference of index value of distance 0 is 3 > 3, so we can skip 0, and so on.
3 > 3-1
2 > 3-2
After finding the core solution to the problem, the general idea is to find out the position of all zeros in the array and determine whether all the numbers before that position can skip the zero position. The code is as follows:
Var canJump = function (nums) {
Var canJump0List = []
For (var I = 0; I
< nums.length - 1; ++i) { if (nums[i] === 0) { //找到0所在位置,标记为false var canJump0 = false; //进行判断,将此位置之前数字进行判断,只要有一个能满足条件就可以跳过这个0 for (var j = i - 1; j >= 0;-- j) {
/ / the value of the player's position must be greater than the distance between the location index value of 0 and the current location index value.
If (nums [j] > I-j) {
CanJump0 = true
Break
}
}
/ / there may be multiple zeros in the array; so the results of all zero judgments are put into one array.
CanJump0List.push (canJump0)
}
}
/ / finally, the array is judged. As long as one does not meet the condition and returns false, it cannot skip to the last.
For (var I = 0; I < canJump0List.length; + + I) {
If (! canJump0List [I]) {
Return false
}
}
Return true
At this point, I believe you have a deeper understanding of "how to solve the jump game algorithm problem with JavaScript". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.