leetcode55. Jump Game
来源:互联网 发布:软件测试职业规划 编辑:程序博客网 时间:2024/06/05 20:20
题目
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) { int n=nums.size(); int i = 0; for (int reach = 0; i < n && i <= reach; ++i) reach = max(i + nums[i], reach); return i == n; }};
0 0
- [LeetCode55]Jump Game
- Leetcode55 Jump Game
- leetcode55 Jump Game
- LeetCode55. Jump Game
- LeetCode55. Jump Game
- LeetCode55 Jump Game
- leetcode55. Jump Game
- leetcode55. Jump Game
- leetcode55-jump game
- leetcode55. Jump Game (贪心)
- LeetCode55. Jump Game
- LeetCode55——Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Jump Game
- Guava缓存值CacheBuilder介绍
- linux系统性能调优第一步——性能分析(vmstat)
- str-...的函数的模拟实现学习
- SpringMVC处理模型数据(1)
- Android编程实现自动检测版本及自动升级的方法
- leetcode55. Jump Game
- EditText 显示明文和密码
- c++实验6
- 【深入Java虚拟机】之五:多态性实现机制——静态分派与动态分派
- 【C语言常识】mdk:Compiler optimization levels and the debug view
- Git自动化部署代码
- Starting MySQL.The server quit without updating PID file (/[失败]ysql/datadir/3306/data/mysql.pid).
- 融云切换账号聊天列表问题
- 关于ajax 跨域请求问题