[leetcode.com]算法题目 - Jump Game
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.
class Solution {
public:
bool canJump(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(==n) return true;
if(==n) return false;
int position = ;
while(position != (n-)){
if( == A[position])
return false;
position += A[position];
if(position >=n)
return true;
}
return true;
}
};
我的答案
思路:这道题由于说明了是non-negative integer,唯一到不了终点的情况就是踩到了值为0的点,不能往后走了。总感觉这道题目有问题,如果最后一步迈的特别大,以至于超过了数组的最大脚标,按online judge的意思是可以算作到达的,我原本理解成为了必须正好踩到最后一个位置上才算是到达~总觉得题目有点问题。
[leetcode.com]算法题目 - Jump Game的更多相关文章
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode.com]算法题目 - Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [leetcode.com]算法题目 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [leetcode.com]算法题目 - Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode.com]算法题目 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode.com]算法题目 - Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...
- [leetcode.com]算法题目 - Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [leetcode.com]算法题目 - Pow(x, n)
Implement pow(x, n). class Solution { public: double pow(double x, int n) { // Start typing your C/C ...
随机推荐
- [ASP.NET]使用Layer简介
layer是一款近年来备受青睐的web弹层组件,她具备全方位的解决方案,致力于服务各水平段的开发人员,您的页面会轻松地拥有丰富友好的操作体验. 在与同类组件的比较中,layer总是能轻易获胜.她尽可能 ...
- python学习 day21 (3月28日)----(抽象类 多态 nametuple dump)
不要因为走的路太久了,而忘记了为了什么而出发. 提前作准备了吗?把思维导图的东西做了吗? 和工作了几年的人,相比,是不是相同的水平,如果要写简历的话. 一边学习,一边复习. 小就是大,少就是多. 1. ...
- 52.tableViewCell重用机制避免重复显示问题
表刷新超出页面显示的内容会重复出现 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSInd ...
- spring boot中注入jpa时报could not autowire.No beans of 'PersonRepository' type found
解决方法,在repository加一个注解.如下图所示: @Component
- Web 开发
Django(发音:[`dʒæŋɡəʊ]) 是一个开放源代码的Web应用框架,由Python写成.采用了MTV的框架模式,模型(Model).模板(Template)和视图(Views).
- Redis和RabbitMQ在项目中的使用
一:Redis的使用 1.先引入pom.xml的依赖 <dependency> <groupId>redis.clients</groupId> <artif ...
- kettle之时间字段默认值为空或’0000-00-00’问题
今天使用kettle从mysql导数到oracle,发现只导了7行后,数据传输就终止了,查看日志信息,报错如下: 报:Couldn't get row from result set问题. 发现从这行 ...
- Codeforces Round#416 Div.2
A. Vladik and Courtesy 题面 At regular competition Vladik and Valera won a and b candies respectively. ...
- what is a resolver
resolver [rɪ'zɒlvə] 解析器 ViewResolver The ViewResolver provides a mapping between view names and actu ...
- leetcode-[3]Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line 思 ...