Balanced Binary Tree @ LeetCode (Python)
Sum Root to Leaf Numbers @ LeetCode (Python)

Jump Game II @ LeetCode (Python)

kitt posted @ 2014年2月18日 19:21 in LeetCode , 2941 阅读

记录最远能到达的范围, 每次在这个范围内搜, 找出下一跳的最远范围, 更新最远范围和跳数。

class Solution:
    # @param A, a list of integers
    # @return an integer
    def jump(self, A):
        lenA = len(A); maxCanReach = 0; jumpNum = 0
        if lenA == 1: return 0
        while True:
            jumpNum += 1
            for i in xrange(maxCanReach + 1):
                maxCanReach = max(maxCanReach, i + A[i])
                if maxCanReach >= lenA - 1: return jumpNum

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter