博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode]403. Frog Jump青蛙过河
阅读量:5322 次
发布时间:2019-06-14

本文共 3762 字,大约阅读时间需要 12 分钟。

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.

Given a list of stones' positions (in units) in sorted ascending order, determine if the frog is able to cross the river by landing on the last stone. Initially, the frog is on the first stone and assume the first jump must be 1 unit.

If the frog's last jump was k units, then its next jump must be either k - 1, k, or k + 1 units. Note that the frog can only jump in the forward direction.

Note:

  • The number of stones is ≥ 2 and is < 1,100.
  • Each stone's position will be a non-negative integer < 231.
  • The first stone's position is always 0.
[0,1,3,5,6,8,12,17]There are a total of 8 stones.The first stone at the 0th unit, second stone at the 1st unit,third stone at the 3rd unit, and so on...The last stone at the 17th unit.Return true. The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone.

 

题意:

一只不会游泳的青蛙,从位置0th unit(即我画的图中的第0号石头)出发开始跳,

若前一次它跳了K units,

这次就只能跳 K-1 或者 K 或者 K+1 units,

问青蛙是否能跳到对岸。

 

 

思路:

以[0,1,3,5,6,8,12,17]为例自己画了个图,我把units理解为直尺上的刻度,更直观理解题意。

用HashMap套一个set,set内存放last jump的units 

首先,遍历一遍给定数组stones = {0,1,3,5,6,8,12,17}, 存入所有给定刻度,初始化HashMap。

Integer(当前刻度)        0    1    3    5   6   8   12   17Set(last jump units)    []  []    []    []  []   []    []    []

 

然后,初始化0刻度对应的Set为[0]。

Integer(当前刻度)         0    1    3    5   6   8   12   17Set(last jump units)    [0]  []    []    []  []   []    []    []

 

 

 最后,遍历一遍给定数组stones = {0,1,3,5,6,8,12,17}

根据题意,青蛙的下一跳(nextJump)范围取决于上一跳(lastJump)

遍历Set里上一跳的取值

Integer(当前刻度)         0    1   3   5   6   8   12   17Set(last jump units)    [0]  []   []   []  []   []    []    [] lastJump                 0nextJump                 -1                         0                         1

 

查找map.containsKey(下一跳的刻度),即 (当前刻度 + nextJump Range)

Integer(当前刻度)         0    1    3    5      6        8        12          17Set(last jump units)    [0]   [1]    [2]     [2]      [3,1]         [3,2]          [4]                [5] lastJump                 0      1       2       2        3   1          3   2           4                    5nestJump Range:(1)lastJump-1           -1      0        1       1        2     0        2      1        3        (2)lastJump              0      1        2       2        3     1        3      2        4       (3)lastJump+1            1      2        3       3        4     2        4      3        5       map.containsKey         0-1✗    1+0✗     3+1✗    5+1✓   6+2✓  6+0✗      8+2✗  8+1✗      12+3✗                        0+0✗    1+1✗     3+2✓    5+2✗   6+3✗  6+1✗      8+3✗  8+2✗      12+4✗                        0+1✓    1+2✓    3+3✓    5+3✓   6+4✗  6+2✓      8+4✓ 8+3✗       12+5✓

                     

代码:

1  public boolean canCross(int[] stones) { 2         if (stones.length == 0) return false; 3         HashMap
> map = new HashMap<>(); 4 for (int i = 0; i < stones.length; i++) { 5 map.put(stones[i], new HashSet<>()); 6 } 7 8 map.get(0).add(0); 9 for (int i = 0; i < stones.length; i++) {10 for (int lastJump : map.get(stones[i])) {11 for (int nextJump = lastJump - 1; nextJump <= lastJump + 1; nextJump++) {12 if (nextJump > 0 && map.containsKey(stones[i] + nextJump)) {13 map.get(stones[i] + nextJump).add(nextJump);14 }15 }16 }17 }18 return !map.get(stones[stones.length - 1]).isEmpty();19 }

 

转载于:https://www.cnblogs.com/liuliu5151/p/9114677.html

你可能感兴趣的文章
无线点餐系统初步构思
查看>>
AJAX
查看>>
前端之CSS
查看>>
List注意点【修改】
查看>>
sqoop导入导出对mysql再带数据库test能跑通用户自己建立的数据库则不行
查看>>
拓扑排序的原理及其实现
查看>>
对StageWebView捕获位图时空白
查看>>
Provison Profile管理及存放路径
查看>>
shop--8.店铺列表展示--前端开发
查看>>
转:Can not issue data manipulation statements with executeQuery()错误解决
查看>>
详解C#委托,事件与回调函数(转)
查看>>
744. Find Smallest Letter Greater Than Target
查看>>
Android 发展思路
查看>>
Sharepoint 自定义字段
查看>>
MySQL 触发器简单实例
查看>>
MySQL------报错Access denied for user 'root'@'localhost' (using password:NO)解决方法
查看>>
车牌识别LPR(三)-- LPR系统整体结构
查看>>
新手村之顺序与分支
查看>>
BZOJ2002: [Hnoi2010]Bounce 弹飞绵羊(LCT)
查看>>
WP8 学习 Onnavigatedto和OnnavigatedFrom的区别
查看>>