-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path55.cpp
More file actions
74 lines (65 loc) · 1.71 KB
/
55.cpp
File metadata and controls
74 lines (65 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Author : Accagain
// Date : 17/3/28
// Email : chenmaosen0@gmail.com
/***************************************************************************************
*
* Given an array of non-negative integers, you are initially positioned at the first index of the array.
*
* Each element in the array represents your maximum jump length at that position.
*
* Determine if you are able to reach the last index.
*
* For example:
*
* A = [2,3,1,1,4], return true.
*
* A = [3,2,1,0,4], return false.
*
* 做法:
*
* 记录在首位置,并且在当前位置,能到达的最大的。
* 不行就跳出来
*
* 时间复杂度:
* O(n)
*
****************************************************************************************/
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#define INF 0x3fffffff
using namespace std;
class Solution {
public:
bool canJump(vector<int>& nums) {
int canFarest = 0, nowStep = 0, ans = 0;
for(int i=0; i<nums.size(); i++)
{
if(i == nowStep)
{
ans++;
canFarest = max(canFarest, nums[i]+i);
nowStep = canFarest;
}
else if(i > nowStep)
break;
canFarest = max(canFarest, nums[i]+i);
//printf("i:%d %d %d\n", i, nowStep, canFarest);
}
if(canFarest >= nums.size()-1)
return true;
return false;
}
};
int main() {
Solution *test = new Solution();
int data[] = {0};
vector<int> x(data, data + sizeof(data) / sizeof(data[0]));
printf("%d\n", test->canJump(x));
return 0;
}
//
// Created by cms on 17/3/28.
//