lc 403. Frog Jump
https://leetcode.com/problems/frog-jump/description/
有一个坑,就是当我为每个石头位置设置一个数组来记录上次跳了多远到现在位置,然后程序给了输入[0,1,2,3,4,5,6........1000] 就超时了,改成dict或者set就行。
code:
class Solution: def canCross(self, stones): """ :type stones: List[int] :rtype: bool """ ss=set(stones) l=len(stones) steps={} steps[1]=set([1]) def add(s,a,last): if a not in steps: s[a]=set([last]) else: # if last not in s[a]: # s[a].add(last) s[a].add(last) if stones[1]!=1: return False for i in range(1,l-1): stn=stones[i] if stn in steps: for last in steps[stn]: if last-1>0 and stn+last-1: add(steps,stn+last-1,last-1) if stn+last in ss: add(steps,stn+last,last) if stn+last+1 in ss: add(steps,stn+last+1,last+1) if stones[-1] in steps: return True return False