题目:

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(vector<int>& nums)
{
return Solution::dfs(nums, );
}
static bool dfs(vector<int>& nums, int index)
{
if ( index>=nums.size()- ) return true;
if ( nums[index]== ) return false;
for ( int i = nums[index] ; i >= ; --i )
{
if ( Solution::dfs(nums, index+i) ) return true;
}
return false;
}
};

tips:

随手写了一个DFS Solution,结果是对的但是超时,shit...

====================================

由于不会Greedy的算法,一心想写一个dp solution,结果最后dp没写成,写成了个greedy;不过代码还是很简洁和高效的:

class Solution {
public:
bool canJump(vector<int>& nums)
{
int max_jump = ;
max_jump = std::max(max_jump, nums[]);
for ( int i = ; i<=max_jump; ++i )
{
if ( max_jump>=nums.size()- ) return true;
max_jump = std::max(max_jump, i+nums[i]);
}
return false;
}
};

tips:

算法的时间复杂度O(n),空间复杂度O(1)。

正常往后迭代变量,每次迭代变量后,维护一个max_jump(即走到元素i,已知可以走到最远的元素下标)。

如果下标大于等于nums.size()-1,则返回true;如果遍历到max_jump了,且没有到达nums.size()-1,则返回false。

后来想想能不能用dp或者dfs再解一次,但想来想去,dp或者dfs解法的核心无非还是变形的greedy,没有太大意思。完毕。

===============================================

第二次过这道题,直接写了greedy的AC了。

class Solution {
public:
bool canJump(vector<int>& nums) {
if ( nums.size()== ) return false;
int maxLength = ;
for ( int i=; i<=maxLength; ++i )
{
if ( i+nums[i]>maxLength )
{
maxLength = i+nums[i];
}
if ( maxLength>=nums.size()- ) return true;
}
return maxLength>=nums.size()-;
}
};

【Jump Game】cpp的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  3. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  6. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  7. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

  8. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  9. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

随机推荐

  1. Openfire+spark在linux上搭建内部聊天系统

    一.    实验环境 Ubuntu server14.04 openfire:http://www.igniterealtime.org/downloads/index.jsp spark:http: ...

  2. mybatis-分页和缓存

    1.分页 1.1在dao接口中配置分页参数: package com.java1234.mappers; import java.util.List;import java.util.Map; imp ...

  3. mysqlbench使用

    看见不少人问mysqlbench怎么用,这个好像没什么困难的,基本看的懂英文就可以使用了,感觉像使用word一样. 下载地址http://www.mysql.com/products/workbenc ...

  4. 免费的freedns实现动态域名和url转发

    路由器的固件是dd-wrt 到freedns.afraid.org上注册一个动态域名,如果默认的端口无法使用80,需要配置二级域名的url转发功能. 实测2个月很稳定. 另外为了防止主机ip地址更新频 ...

  5. python_70_内置函数2

    #hex() 转16进制 oct()转8进制 print(hex(255)) print(oct(10)) #id() 返回内存地址 print(id('a')) ''' isinstance(obj ...

  6. oo作业第四单元总结暨结课总结

    目录 一.第四单元作业架构设计 1.第一次UML作业架构设计 2.第二次UML作业架构设计 二.架构设计和OO方法理解演进 三.测试理解与实践的演进 四.课程收获总结 五.三个具体改进建议 一.第四单 ...

  7. ubuntu安装R时候增加软件源到sources.list,sudo apt-get update不能更新

    http://forum.ubuntu.org.cn/viewtopic.php?t=401717 ubuntu安装R时候增加软件源到sources.list,sudo apt-get update不 ...

  8. javase(9)_java io系统

    一.File类 1.file既可以代表一个特定文件的名称,又可以代表一个目录下的一组文件的名称,实际上,FilePath对这个类来说是个更好的名字.2.目录列表器例: import java.io.F ...

  9. angular2新建组件

    1,使用ng g c hello 创建一个新的组件 它创建了4个文件,并更新了app.module.ts 如果想访问这个组件,只需要添加它的路由 成功访问这个组件 Import语句定义了我们需要用到的 ...

  10. Drop it-freecodecamp算法题目

    Drop it 1.要求 丢弃数组(arr)的元素,从左边开始,直到回调函数return true就停止. 第二个参数,func,是一个函数.用来测试数组的第一个元素,如果返回fasle,就从数组中抛 ...