首页 > 其他分享 >LeetCode 62 不同路径

LeetCode 62 不同路径

时间:2022-09-22 16:57:34浏览次数:59  
标签:int res 路径 dfs ++ 62 LeetCode dp

dfs(超时)


class Solution {
public:
    int res = 0;

    void dfs(int x, int y, int m, int n) {

        if (x == m && y == n) res ++;

        if (x + 1 <= m) dfs(x + 1, y, m, n);
        if (y + 1 <= n) dfs(x, y + 1, m, n);
      

    }
    int uniquePaths(int m, int n) {
        dfs(1, 1, m, n);

        return res;
    }
};

动态规划

// 7 * 3
/*
1 1 1 1  1  1  1
1 2 3 4  5  6  7
1 3 6 10 15 21 28
*/

const int N = 110;
class Solution {
public:
    int dp[N][N];
    int uniquePaths(int m, int n) {
        for (int i = 0; i < m; i ++) dp[i][0] = 1;
        for (int i = 0; i < n; i ++) dp[0][i] = 1;

        for (int i = 1; i < m; i ++)
            for (int j = 1; j < n; j ++)
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];

        return dp[m - 1][n - 1];
    }
};

标签:int,res,路径,dfs,++,62,LeetCode,dp
From: https://www.cnblogs.com/hjy94wo/p/16719914.html

相关文章

  • leetcode 1640. 能否连接形成数组
    //1class Solution {    int cnt=0;    public boolean canFormArray(int[] arr, int[][] pieces) {        return dfs(0,arr,pieces); ......
  • LeetCode 746 使用最小花费爬楼梯
    constintN=1000;classSolution{public:intdp[N];intminCostClimbingStairs(vector<int>&cost){dp[0]=cost[0];dp[1]=cost[1]......
  • LeetCode 70 爬楼梯
    动态规划constintN=50;classSolution{public:intdp[N];intclimbStairs(intn){dp[0]=1;dp[1]=1;for(int......
  • LeetCode 509 斐波那契数
    动态规划constintN=40;classSolution{public:intdp[N];intfib(intn){dp[1]=1;for(inti=2;i<=n;i++)......
  • leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与
    一、题目大意给定两个整数数组preorder和inorder,其中preorder是二叉树的先序遍历,inorder是同一棵树的中序遍历,请构造二叉树并返回其根节点。示例1:输入:pre......
  • shell脚本中获取当前脚本的绝对路径
    通过对参数扩展的形式直接获取shell脚本路径并进入其中##获取当请可执行脚本的名称和路径##$0##${变量%/*}通过%参数扩展的方式,删除第一个匹配到的/右方全部内容(*)......
  • 代码随想录 数组理论基础,二分查找,移除元素 及 LeetCode 704, 27
    数组理论基础数组是存放在连续内存空间上的相同类型数据的集合。数组下标都是从0开始的。数组内存空间的地址是连续的因为数组的在内存空间的地址是连续的,所以我们在......
  • 设置自己的动态库路径的几种方式
    1.放到系统路径下这种容易破坏环境,不建议2.设置环境变量LD_LIBARAY_PATHLD_LIBARAY_PATH=LD_LIBARAY_PATH:库路径3.添加路径到/etc/ld.so.conf添加路径到/et......
  • 9.21Leetcode记录
    一、数据流中的中位数题目如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那......
  • AcWing 算法提高课 欧拉回路和欧拉路径
    定义:经过每一条边且每一条边恰好只经过一次一、无向图中,当所有边都连通时:存在欧拉路径,等价于,图中度为奇数的点只有0或2个。存在欧拉回路,等价于,图中度为奇数的点只有0个......