【leetcode】403. Frog Jump
题目如下:
解题思路:我的做法是建立一个字典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的更多相关文章
- 【Leetcode】经典的Jump Game in JAVA
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【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 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- 测开之路一百零一:jquery文字特效、动画、方法链
文字特效 html内容 1.卷起/展开 2.隐藏/显示 3.淡入淡出 <!DOCTYPE html><html lang="en"><head> ...
- 函数介绍——MulDiv
http://blog.sina.com.cn/s/blog_579ebc11010008ql.html 函数介绍——MulDiv (2007-03-27 10:05:30) 转载▼ 分类: 编程 ...
- 16/7/8_PHP-字符串介绍
不知道为什么慕课网还这个都要介绍.不过还是一个新的知识点. PHP开发中,我们遇到最多的可能就是字符串. 字符串变量用于包含字符串的值. 一个字符串 通过下面的3种方法来定义: 1.单引号2.双引号3 ...
- 如何快速查找到多个字典中的公共键(Key)---Python数据结构与算法相关问题与解决技巧
如何快速查找到多个字典中的公共键(Key)-? 实际案例: 西班牙足球甲级联赛,每轮球员进球统计: 第1轮: { '苏亚雷斯':1,'梅西':2,'本泽马':1,...} 第2轮: { '苏亚雷斯 ...
- spring data jpa 使用SQL语句查询
package com.ytkj.entity; import javax.persistence.*; import java.io.Serializable; /** * @Entity * 作用 ...
- ntp局域网时间同步操作
需求:局域网里面有两台电脑需要同步时间 一台windows,一台Linux.把windows当作服务器 windows10自带ntp服务器,可以按如下步骤进行设置 1. 打开注册表编辑器,在运行里面输 ...
- java面向对象基础总结
本周学习了java面向对象的一些基本概念,介绍了它三个主要特性,封装性.继承性.多态性,类与对象的关系,栈堆的关系,三个特性中主要讲了封装性,其他两个后面再讲. 类实际上是表示一个客观世界某类群体的一 ...
- [Python3 填坑] 010 isalpha() 对于字母的定义
目录 1. print( 坑的信息 ) 2. 开始填坑 官方文档 1. print( 坑的信息 ) 挖坑时间:2019/01/14 明细 坑的编码 内容 Py009-1 isalpha() 理当只有输 ...
- <每日一题> Day2:CodeForces-1141C.PolycarpRestoresPermutation(思维题)
原题链接 参考代码: #include <iostream> #include <cstring> using namespace std; + , INF = 0x3f3f3 ...
- dp(传球)
https://ac.nowcoder.com/acm/contest/1126/B 链接:https://ac.nowcoder.com/acm/contest/1126/B来源:牛客网 上体育课的 ...