Array - 55. Jump Game
时间:2022-07-25
本文章向大家介绍Array - 55. Jump Game,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
55、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.
Example 1:
Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
视频讲解: https://www.bilibili.com/video/BV1154y1D7m2
思路:
题目是说给定一个数组,每一位数字代表可以往前走几步,能走到最后一位就返回true。可以记录下每一步之后可以走的最大距离,然后更新这个最大距离,一次遍历就可以。
代码:
java:
class Solution {
public boolean canJump(int[] nums) {
if(nums == null || nums.length <= 1) return true;
int max = 0;
for (int i = 0; i < nums.length && i <= max; i++) {
max = Math.max(nums[i] + i, max);
if (max >= nums.length-1) return true;
}
return false;
}
}
go:
func canJump(nums []int) bool {
if nums == nil || len(nums) <= 1 {return true}
reach := 0
for i := 0; i < len(nums) && i <= reach; i++ {
reach = func (x, y int) int {
if x > y { return x }
return y
}(nums[i] + i, reach)
if reach >= len(nums) -1 {return true}
}
return false
}
- ASP.NET MVC下的四种验证编程方式
- 总体介绍ASP.NET Web API下Controller的激活与释放流程
- 如何让ASP.NET Web API的Action方法在希望的Culture下执行
- ASP.NET Web API标准的“管道式”设计
- ASP.NET Web API路由系统:Web Host下的URL路由
- ASP.NET Web API路由系统:路由系统的几个核心类型
- [ASP.NET] 如果将缓存“滑动过期时间”设置为1秒会怎样?
- [ASP.NET MVC] 利用自定义的AuthenticationFilter实现Basic认证
- [CORS:跨域资源共享] 同源策略与JSONP
- [CORS:跨域资源共享] W3C的CORS Specification
- 通过扩展让ASP.NET Web API支持JSONP
- ASP.NET Web API自身对CORS的支持:从实例开始
- Generator:化异步为同步
- 超详细的大数据学习资源推荐(下)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- python递归函数
- Mysql 游标
- python小工具,通讯录小程序
- Python3基础:集合的使用
- 删除处于Terminating状态的namespace
- elasticSearch学习(七)
- Flutter透明度渐变动画FadeTransition实现透明度渐变动画效果
- iOS NSTimer不走的问题
- 我对python中的super()机制的一点理解
- 【Python】使用Pygame做一个Flappy bird小游戏(五)
- 【动手学深度学习笔记】之自定义层
- Ubuntu18.04系统安装和必备软件安装指南
- 如何查看微信好友已撤回的消息?
- 【Python爬虫】写一个爬取中国天气网的终端版天气预报爬虫
- 如何制定企业级代码规范与检查