- 浏览: 183682 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
一道简单的二维动态规划的问题,构造一个二维数组dp[][],当机器人走到(i,j)点的时候,因为机器人只能向右和向下两个方向走,这时总的路径就为dp[i][j] = dp[i - 1][j] + dp[i][j - 1],(i >= 1, j >= 1)。当i = 0 或j = 0的时候都只有一种走法,dp[0][j] = 1; dp[i][0] = 1,这是初始化。上面我们已经写出了递推式,这道题就解决了。代码如下:
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.
一道简单的二维动态规划的问题,构造一个二维数组dp[][],当机器人走到(i,j)点的时候,因为机器人只能向右和向下两个方向走,这时总的路径就为dp[i][j] = dp[i - 1][j] + dp[i][j - 1],(i >= 1, j >= 1)。当i = 0 或j = 0的时候都只有一种走法,dp[0][j] = 1; dp[i][0] = 1,这是初始化。上面我们已经写出了递推式,这道题就解决了。代码如下:
public class Solution { public int uniquePaths(int m, int n) { 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]; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 499Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 430Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 581Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 676Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 845Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 706For a undirected graph with tre ...
相关推荐
1、记忆化 2、二维dp 3、滚动优化
javascript js_leetcode题解之62-unique-paths.js
c语言入门 C语言_leetcode题解之62-unique-paths.c
public static int uniquePaths(int rows, int cols, int[][] grid) { int[][] dp = new int[rows][cols]; // 初始化第一列全部为1,表示只有一条路径到达每个点 for (int i = 0; i ; i++) { if (grid[i][0] =...
javascript js_leetcode题解之63-unique-paths-ii.js
c语言入门 C语言_leetcode题解之63-unique-paths-ii.c
44. Unique Paths II:与 Unique Paths 类似,但是考虑网格中障碍物的处理。 45. Maximum Sum Subarray:找出一个数字序列中和最大的连续子序列。 46. Maximum Product Subarray:找出一个数字序列中乘积最大的连续...
- "不同路径"(Unique Paths)和"不同路径 II"(Unique Paths II)同样是关于路径计算的动态规划问题。 **二分搜索(Binary Search)** 二分搜索算法适用于有序数组或列表的快速查找问题。 - "搜索插入位置"...
44. Unique Paths II:不同路径数的计算,其中包含障碍物。 45. Maximum Sum Subarray:计算子数组的最大和。 46. Maximum Product Subarray:计算子数组的最大乘积。 47. Coins in a Line:动态规划解决博弈问题,...
Unique Paths Unique Paths II Longest Palindromic Substring Interleaving String Triangle Distinct Subsequences Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II...
def uniquePaths(m, n): dp = [[1] * n] + [[1] + [0] * (n - 1) for _ in range(m - 1)] for i in range(1, m): for j in range(1, n): dp[i][j] = dp[i - 1][j] + dp[i][j - 1] return dp[m - 1][n - 1] ``...
例如,"Unique Paths"(不同路径)和"House Robber"(打家劫舍)都是经典的动态规划题目。 3. **回溯与深度优先搜索**:这类算法通常用于解决组合优化问题和图遍历问题。例如,"N-Queens"(八皇后问题)和"Word ...
- Unique Paths(不同路径) 11. Binary Search(二分查找): 二分查找是一种在有序数组中查找特定元素的搜索算法。文件中出现的二分查找相关题目包括: - Search Insert Position(搜索插入位置) - Find ...
int uniquePaths(int m, int n) { if(!n || !m) return 0; vector<vector<int>>f(m + 1, vector(n + 1)); f[0][0] = 1; for(int i = 0; i ; i++) for(int j = 0; j ; j++){ if(!i && !j) continue; if(i) f...
- Unique Paths / Unique Paths II: 前者计算从矩阵的左上角到右下角的路径数量;后者则考虑了障碍物。 - Minimum Path Sum: 在一个m×n的网格中,找到从左上角到右下角的路径,使得路径上的数字总和最小。 - Plus ...
2. **不同路径(Unique Paths)**:给定一个机器人位于一个m x n网格的左上角,求出到达网格右下角的不同路径数量。 ### 二分查找 二分查找是解决有序数组或列表中特定问题的有效算法。 1. **搜索插入位置(Search...
62.Unique Paths, 63.Unique Paths II, 64.Minimum Path Sum 2017/03/09 70.Climbing Stairs, 198.House Robber, 55.Jump Game 72.Edit Distance, 97.Interleaving String, 115.Distinct Subsequences 2017/04/24 ...
- **Unique Paths**:计算机器人到达目标位置的唯一路径数。 - **Maximum Subarray**:寻找数组中的最大子数组和。 - **Climbing Stairs**:模拟爬楼梯,每次可以爬1阶或2阶。 5. **回溯(Backtracking)**: -...