45. Jump Game II
2016-06-07 10:04
197 查看
枚举出所有到达终点的情况 最终超时了
最终的解法
class Solution { private: int minstep=INT_MAX; void dfs(vector<int>&nums,int index,int cstep) { if(index<nums.size()-1)//没有到达终点则枚举出下一步所有可能的步长 { for(int i=1;i<=nums[index];i++) { dfs(nums,index+i,cstep+1); } }else if(index==nums.size()-1)//到了终点 则更新最小的步数 { minstep=min(minstep,cstep); } } public: int jump(vector<int>& nums) { dfs(nums,0,0); return minstep; } };
最终的解法
class Solution { public: int jump(vector<int>& nums) { int level=-1; int currentmax=0; int nextmax=0; int i=0; int n=nums.size(); while(i<=currentmax&&i<n) { level++; nextmax=currentmax; for(;i<=currentmax&&i<n;i++) { nextmax=max(nextmax,nums[i]+i); if(i>=n-1) return level; } currentmax=nextmax; } return 0; } };
相关文章推荐
- 01-shell文本处理三剑客之grep
- Python网页抓取入门小示例
- Flex4重写动态更换行背景datagrid
- javascript函数作用域和嵌套调用关系
- robot.libdocpkg package
- bootstrap输入框组代码分享
- iOS 中使用JS提取网页数据,自动提交表单等
- iOS通过经纬度计算两个地点之间的距离
- centos 7 Tomcat9 Jdk8
- thinkphp 常量的定义
- 【LQ系列】 BASIC-20~BASIC-25
- jmeter 实时搜索结果
- 如何在Windows7上完全卸载Oracle 11g(转)
- chrome打不开网页显示ERR_NAME_NOT_RESOLVED错误的解决方法
- Java学习之InputStream中read()与read(byte[] b)
- matlab实现CP分解 代码
- js生成日志信息及实现java直接调用flume
- Centos下的ftp安装与配置
- 机器学习中常见的几种最优化方法
- C/C++文件读与写(函数fopen,fwrite,fprintf,fgetc,fputc,fgets,fclose)