[leetcode]62.UniquePaths

/**
 * Created by lvhao on 2017/7/6.
 * 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?
 动态规划,目标是到最终点的路径有几条。每个点的路径都是能到这个点的上一个点的路径之和,由于
 只能向下和向右走,所以动态方程是A[i][j] = A[i][j-1] + A[j][j+1]
 算出每个点的值就行,最后返回A[m-1][n-1]
 */
public class Q62UniquePaths {
    public static void main(String[] args) {
        System.out.println(uniquePaths(3,3));
    }
    public static int uniquePaths(int m, int n) {
        if (m == 1 || n == 1)
            return 1;
        int[][] res = new int[m][n];
        //一开始要先把二维数组上和左边的初始化为1,这是初始条件
        for (int i = 0; i < m; i++) {
            res[i][0] = 1;
        }
        for (int i = 0; i < n; i++) {
            res[0][i] = 1;
        }
        //动态规划
        for (int i = 1; i < m; i++) {
            for (int j = 1;j < n;j++)
            {
                res[i][j] = res[i-1][j] + res[i][j-1];
            }

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

}
时间: 2024-10-25 22:09:27

[leetcode]62.UniquePaths的相关文章

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/63/120 Unique PathsI/II Triangle--DP

一:unique Path 题目: 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. 不同路径

62. 不同路径 我们直接用递归来模拟 class Solution { public static int cnt; public int uniquePaths(int m, int n) { cnt = 0; dfs(1, 1, m, n); return cnt; } public void dfs(int i, int j, int m, int n) { if (i == m && j == n) { cnt++; return; } if ((j + 1) <= n)

LeetCode 62, 63 Unique Paths I+II

62. Unique Paths 空间可以按行优化为 O(n),也可以按列优化为O(m). class Solution { public: int uniquePaths(int m, int n) { int dp[m][n]={0}; dp[0][0]=1; for (int i=0;i<m;++i){ for (int j=0;j<n;++j){ if (i==0&&j==0) continue; dp[i][j]=0; if (j-1>=0) dp[i][j]

LeetCode.62——不同路径

问题描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为"Finish"). 问总共有多少条不同的路径? 例如,上图是一个7 x 3 的网格.有多少可能的路径?LeetCode原题 问题分析: 这是一个比较简单的动态规划问题,由于没有障碍 (不同路径2 网格中有障碍), 由于每一步都只能向右或者向下,那很明显,可以知道第一行和第一列的每一个格子

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'

leetcode || 62、Unique Paths

problem: 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 'Fin

[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 (marke