Golang | Leetcode Golang题解之第403题青蛙过河

题目:

题解:

Go 复制代码
func canCross(stones []int) bool {
    n := len(stones)
    dp := make([][]bool, n)
    for i := range dp {
        dp[i] = make([]bool, n)
    }
    dp[0][0] = true
    for i := 1; i < n; i++ {
        if stones[i]-stones[i-1] > i {
            return false
        }
    }
    for i := 1; i < n; i++ {
        for j := i - 1; j >= 0; j-- {
            k := stones[i] - stones[j]
            if k > j+1 {
                break
            }
            dp[i][k] = dp[j][k-1] || dp[j][k] || dp[j][k+1]
            if i == n-1 && dp[i][k] {
                return true
            }
        }
    }
    return false
}
相关推荐
Kuo-Teng2 小时前
LeetCode 206: Reverse Linked List
java·算法·leetcode·职场和发展
做怪小疯子6 小时前
LeetCode 热题 100——哈希——最长连续序列
算法·leetcode·哈希算法
Dream it possible!6 小时前
LeetCode 面试经典 150_二叉树_二叉树展开为链表(74_114_C++_中等)
c++·leetcode·链表·面试·二叉树
做怪小疯子6 小时前
LeetCode 热题 100——双指针——三数之和
算法·leetcode·职场和发展
sin_hielo7 小时前
leetcode 2536
数据结构·算法·leetcode
flashlight_hi7 小时前
LeetCode 分类刷题:203. 移除链表元素
算法·leetcode·链表
py有趣7 小时前
LeetCode算法学习之数组中的第K个最大元素
学习·算法·leetcode
吗~喽7 小时前
【LeetCode】将 x 减到 0 的最小操作数
算法·leetcode
stand_forever9 小时前
PHP客户端调用由Go服务端GRPC接口
rpc·golang·php
flashlight_hi10 小时前
LeetCode 分类刷题:3217. 从链表中移除在数组中存在的节点
javascript·数据结构·leetcode·链表