您的位置:首页 > 编程语言 > C语言/C++

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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: