题目如下:

解题思路:我的做法是建立一个字典dic,key为stone,value是一个set,里面存的是从前面的所有stone跳跃到当前stone的unit集合。例如stones=[0,1,2,3]。stone3可以从stone1跳跃两步得到或者从stone2跳跃1步得到,所有dic[3] = (2,1)。那么从stone3可以跳的unit就是[1,2,3] (set中每个元素+1或者-1得到),通过stone3就能到达stone4,stone5,stone6。这样只要按顺序遍历stones,最后判断len(dic[stones[-1]]是否大于0即可。

代码如下:

class Solution(object):
def canCross(self, stones):
"""
:type stones: List[int]
:rtype: bool
"""
dic = {}
for i in stones:
dic[i] = set()
dic[0].add(1)
for i in stones:
for j in dic[i]:
if j+i in dic:
dic[j+i].add(j)
if i != 0 and j+i+1 in dic:
dic[j+i+1].add(j+1)
if j-1 > 0 and j + i - 1 in dic:
dic[j + i - 1].add(j - 1)
#print dic
return len(dic[stones[-1]]) > 0

【leetcode】403. Frog Jump的更多相关文章

  1. 【Leetcode】经典的Jump Game in JAVA

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. hypermesh中怎么设置支反力(反作用力)

    Analysis page >> Control cards >> Global output request 勾选 SPCF 和 GPFORCE .

  2. 局域网IP耗尽

    w 大量的vm  大量的实例  动态修改 mac

  3. (转)datagridview 自定义列三步走

    本文转载自:http://blog.csdn.net/zx13525079024/article/details/4814642 我们如果想自定义实现datagridview的某列,例如是datagr ...

  4. WebSocket知识、轮询、长轮询、长连接

    一.WebSocket理论知识 1.什么是websocket WebSocket是HTML5新增的协议,它的目的是在浏览器和服务器之间建立一个不受限的双向通信的通道,比如说,服务器可以在任意时刻发送消 ...

  5. Delphi XE2 之 FireMonkey 入门(25) - 数据绑定: TBindingsList: 表达式的灵活性及表达式函数

    Delphi XE2 之 FireMonkey 入门(25) - 数据绑定: TBindingsList: 表达式的灵活性及表达式函数 绑定表达式中可以有简单的运算和字符串连接, 但字符串需放在双引号 ...

  6. Python 爬取淘宝商品数据挖掘分析实战

    Python 爬取淘宝商品数据挖掘分析实战 项目内容 本案例选择>> 商品类目:沙发: 数量:共100页  4400个商品: 筛选条件:天猫.销量从高到低.价格500元以上. 爬取淘宝商品 ...

  7. GUI_FlowLayout

    void setBounds(x, y, width, height) 设置窗体坐标,窗体大小 import java.awt.Frame; public class IntegerDemo { pu ...

  8. c++静态成员变量初始化时不受访问权限控制

    1.要在类外初始化,const 成员变量才能在类内初始化 2.初始化在类外,而不在main函数内 class A{ private: string name; A(){ name = "a& ...

  9. 剑指offer--day08

    1.1 题目:二叉树镜像:操作给定的二叉树,将其变换为源二叉树的镜像. 1.2 思路:先交换根节点的两个子结点之后,我们注意到值为10.6的结点的子结点仍然保持不变,因此我们还需要交换这两个结点的左右 ...

  10. 应用安全 - 软件漏洞 - Jira漏洞汇总

    CVE-2019-8451 ssrf url = url + '/plugins/servlet/gadgets/makeRequest?url=' + host + '@www.baidu.com/ ...