[LeetCode] 62.Unique Paths I

Unique Paths I

A robot is located at the top-left corner of a m x n grid (marked ‘Start‘ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish‘ in the diagram below).

How many possible unique paths are there?

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

解题思路:

Climbing Stairs二维版。计算解个数的题多半是用DP。而这两题状态也非常显然,dp[i][j]表示从起点到位置(i, j)的路径总数。DP题目定义好状态后,接下去有两个任务:找通项公式,以及确定计算的方向。

1. 由于只能向右和左走,所以对于(i, j)来说,只能从左边或上边的格子走下来:

dp[i][j] = dp[i-1][j] + dp[i][j-1]

2. 对于网格最上边和最左边,则只能从起点出发直线走到,dp[0][j] = dp[i][0] = 1

3. 计算方向从上到下,从左到右即可。可以用滚动数组实现。

Java Solution 1:

class Solution {
  public int uniquePaths(int m, int n) {
    if (m == 0 || n == 0) {
        return 1;
    }

    int[][] dp = new int[m][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];
  }

  public static void main(String args[]) {
    Solution sol = new Solution();
    System.out.println(sol.uniquePaths(3, 3));
  }
}

  

Java Solution 2:

class Solution {
  public int uniquePaths(int m, int n) {
    int[][] dp = new int[m][n];
    int i, j;
    for (i = 0; i < m; ++i) {
      for (j = 0; j < n; ++ j) {
        if (i == 0 || j == 0) {
          dp[i][j] = 1;
        }
        else {
          dp[i][j] = dp[i-1][j] + dp[i][j-1];
        }
      }
    }
    return dp[m-1][n-1];
  }
}

  

  

原文地址:https://www.cnblogs.com/lightwindy/p/8469117.html

时间: 2024-10-13 11:13:01

[LeetCode] 62.Unique Paths I的相关文章

LeetCode --- 62. Unique Paths

题目链接:Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (ma

leetCode 62.Unique Paths (唯一路径) 解题思路和方法

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in t

LeetCode 62. Unique Paths Java

题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish'

19.2.9 [LeetCode 62] Unique Paths

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in t

[Leetcode]@python 62. Unique Paths

题目链接:https://leetcode.com/problems/unique-paths/ 题目大意:给定n.m,在mxn的矩阵中,从(0,0)走到(m-1,n-1)一共有多少种法(只能往下和往右走) 解题思路:从(0,0)到(m-1,n-1)一共要走m - 1次向下,n-1次向右.也就是在n + m - 2次中选出m-1次向下,也就是C(m + n - 2,m-1) class Solution(object): def uniquePaths(self, m, n): ""&

LeetCode --- 63. Unique Paths II

题目链接:Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one

【Leetcode】Unique Paths

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in t

【Leetcode】Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

62. Unique Paths i &amp; ii

62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (mar