【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 the diagram below).

Note: m and n will be at most 100.

【解析】

题意:数组 A[m][n] ,从 A[0][0] 到 A[m-1][n-1] 有多少条路径。

动态规划不熟的同学,可以借这个例子由浅入深理解一下。

二维数组实现:

public class Solution {
    public int uniquePaths(int m, int n) {
        // DP with 2 dimensions array
        int[][] a = new int[m][n];
        for (int i = 0; i < m; i++) {
            a[i][0] = 1;
        }
        for (int i = 0; i < n; i++) {
            a[0][i] = 1;
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                a[i][j] = a[i-1][j] + a[i][j-1];
            }
        }
        return a[m-1][n-1];
    }
}

一维数组实现:

public class Solution {
    public int uniquePaths(int m, int n) {
        // DP with 1 dimension array
        int[] a = new int[n];
        for (int j = 0; j < n; j++) {
            a[j] = 1;
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                a[j] += a[j-1];
            }
        }
        return a[n-1];
    }
}

一维数组升级版:

public class Solution {
    public int uniquePaths(int m, int n) {
        // Advanced: DP with 1 dimension array
    	int row = Math.min(m, n);
        int col = Math.max(m, n);
        int[] a = new int[col];
        for (int j = 0; j < col; j++) {
            a[j] = 1;
        }
        for (int i = 1; i < row; i++) {
            a[i] *= 2;
            for (int j = i+1; j < col; j++) {
                a[j] += a[j-1];
            }
        }
        return a[col-1];
    }
}
时间: 2024-10-12 15:20:44

【LeetCode】Unique Paths 解题报告的相关文章

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 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution 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 spac

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

[leetcode]Unique Paths II @ Python

原题地址:https://oj.leetcode.com/problems/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 i

[leetcode]Unique Paths @ Python

原题地址:https://oj.leetcode.com/problems/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 t

soj 1015 Jill&#39;s Tour Paths 解题报告

题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every year, Jill takes a bicycle tour between two villages. There are different routes she can take between these villages, but she does have an upper limit

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

[LeetCode] Unique Paths II(DP)

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