leetcode_62题——Unique Paths (动态规划)

Unique Paths

Total Accepted: 45580 Total Submissions: 138958My Submissions

Question Solution

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.

Hide Tags

Array Dynamic Programming

这道题比较简单,就是采用动态规划的做法,需要维护一个路径个数的数组,从靠近终点的点开始往前算起就好了

#include<iostream>
#include <vector>
using namespace std;
#define MAXSIZE 110
int uniquePaths(int m, int n) {
	int ary1[MAXSIZE][MAXSIZE];
	ary1[m][n]=1;
	for(int i=m;i>=1;i--)
		for(int j=n;j>=1;j--)
		{
			if(i==m||j==n)
				ary1[i][j]=1;
			else
				ary1[i][j]=ary1[i+1][j]+ary1[i][j+1];
		}
		return ary1[1][1];
}

int main()
{
}

  

时间: 2024-08-30 02:54:51

leetcode_62题——Unique Paths (动态规划)的相关文章

leetcode_63题——Unique Paths II(动态规划)

Unique Paths II Total Accepted: 35061 Total Submissions: 125276My 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] Unique Paths &amp;&amp; Unique Paths II &amp;&amp; Minimum Path Sum (动态规划之 Matrix DP )

Unique Paths 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 rea

Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在考虑网格中有障碍物.那么从左上角到右下角将会有多少条不同的路径? 网格中的障碍物和空位置分别用 1 和 0 来表示. 说明:m 和 n 的值均不超过 100. 示例 1: 输入: [   [0,0,0],  

LeetCode之“动态规划”:Minimum Path Sum &amp;&amp; Unique Paths &amp;&amp; Unique Paths II

之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or righ

【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

原题链接在这里:980. Unique Paths III

原题链接在这里:https://leetcode.com/problems/unique-paths-iii/ 题目: On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  There is exactly one starting square. 2 represents the ending square.  There is exactly one ending s

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

Leetcode 动态规划 Unique Paths II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Paths II Total Accepted: 13655 Total Submissions: 49081My Submissions Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would t

Leetcode 动态规划 Unique Paths

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Paths Total Accepted: 17915 Total Submissions: 57061My Submissions 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 e