leetcode c++ jump game 2
2014-11-10 17:19
337 查看
I declare two para ; boundary start and boundary end and for each step I update the new boundary start and the boundary end.
if the boundary end >= n-1 we get the signal that we can reach the tail of the whole array.
However, you must pay attention to a situation, that in this step, we cannot get a further tail.
eg.
5 9 3 2 1 0 1 1 1 1 0 0
so that we still cannot reach to the tail.
so we must judge if in this step, we cannot have a further tail, we must return 0;class Solution {
public:
int jump(int A[], int n){
vector <int> temp;
int step = 0;
if(n == 0 || n == 1)
return step;
int size = 0;;
int i = 0;
int boundrystart = 0;
int boundryend = A[0];
size = boundryend-boundrystart;
while(boundrystart <= boundryend)
{
step ++;
if(boundryend >=n-1 )
return step;
for(int i =boundrystart+1; i<= size+boundrystart;i++)
{
if(i+A[i]>boundryend)
boundryend = A[i]+i;
}
if(boundryend == boundrystart + size)
return 0;
boundrystart = boundrystart + size;
size = boundryend - boundrystart;
}
return 0;
}
};
if the boundary end >= n-1 we get the signal that we can reach the tail of the whole array.
However, you must pay attention to a situation, that in this step, we cannot get a further tail.
eg.
5 9 3 2 1 0 1 1 1 1 0 0
so that we still cannot reach to the tail.
so we must judge if in this step, we cannot have a further tail, we must return 0;class Solution {
public:
int jump(int A[], int n){
vector <int> temp;
int step = 0;
if(n == 0 || n == 1)
return step;
int size = 0;;
int i = 0;
int boundrystart = 0;
int boundryend = A[0];
size = boundryend-boundrystart;
while(boundrystart <= boundryend)
{
step ++;
if(boundryend >=n-1 )
return step;
for(int i =boundrystart+1; i<= size+boundrystart;i++)
{
if(i+A[i]>boundryend)
boundryend = A[i]+i;
}
if(boundryend == boundrystart + size)
return 0;
boundrystart = boundrystart + size;
size = boundryend - boundrystart;
}
return 0;
}
};
相关文章推荐
- Leetcode_jump-game (updated c++ and python version)
- Leetcode_jump-game-ii(c++ and python version)
- leetcode_c++:Jump Game II(045)
- [C++]LeetCode: 104 Jump Game II (局部最优和全局最优法)
- leetcode Jump Game
- leetcode jump game I&&II
- LeetCode题目 Jump Game II
- LeetCode: Jump Game II
- [Leetcode] Jump Game ii
- [Leetcode] Jump Game II
- Leetcode:Jump Game II
- LeetCode Jump Game
- [LeetCode] Jump Game II
- [LeetCode]Jump Game II
- [leetcode]Jump Game II
- LeetCode_Jump Game II
- [leetcode]Jump Game II
- [leetcode]Jump Game II
- leetcode: Jump Game II
- [leetcode] Jump Game II