关闭

LeetCode Unique Paths

标签: leetcode
165人阅读 评论(0) 收藏 举报
分类:

题目:

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?

Note: m and n will be at most 100.

题意:

给定一个m*n的格子,每一个格子只能向右走或者是向下走,然后计算从最左上的那个格子开始走到最右下的那个格子,共有几种走法?此题考虑典型的动态规划,而且此题与之前的关于走格子计算最大值和最小值都很像,但是此题要注意的是,最上面的那一行和最左边的那一列都是1,因为只有一种走法,所以只有1.其他与之前的那种类型一样。

public class Solution 
{
    public static int uniquePaths(int m,int n)
	{
		int[][] nums = new int[m][n];
		nums[0][0] = 1;
		for(int i = 0; i < m; i++)
		{
			for(int j = 0; j < n; j++)
			{
				if(i > 0 && j > 0)
					nums[i][j] = nums[i-1][j] + nums[i][j-1];
				if(i == 0 && j > 0)
					nums[i][j] = 1;
				if(i > 0 && j == 0)
					nums[i][j] = 1;
			}
		}
		return nums[m-1][n-1];
	}
}



0
0
查看评论

【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 onl...
  • ljiabin
  • ljiabin
  • 2014-12-08 17:19
  • 4063

LeetCode-62-Unique Paths(动态规划)-Medium

题目理解: 在只允许从左向右,从上到下移动的情况下,从mxn的矩阵(0,0)点,到(m-1,n-1)点,有多少路径; 题目分析: 1. 回溯遍历会超时; 2. 动规; 解题代码: public class Solution { public int uniqueP...
  • eddy_liu
  • eddy_liu
  • 2016-03-14 08:51
  • 1101

Unique Paths II -- LeetCode

原题链接: http://oj.leetcode.com/problems/unique-paths-ii/  这道题跟Unique Paths非常类似,只是这道题给机器人加了障碍,不是每次都有两个选择(向右,向下)了。因为有了这个条件,所以Unique Paths中最后一个...
  • linhuanmars
  • linhuanmars
  • 2014-03-26 04:15
  • 10712

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 ...
  • jiadebin890724
  • jiadebin890724
  • 2014-04-09 22:17
  • 6935

【LeetCode with Python】 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...
  • nerv3x3
  • nerv3x3
  • 2014-08-29 18:11
  • 2464

LeetCode 63 — Unique Paths II(C++ Java Python)

题目:http://oj.leetcode.com/problems/unique-paths-ii/ Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How...
  • dragon_dream
  • dragon_dream
  • 2014-02-25 22:19
  • 2068

leetcode 62. Unique Paths-唯一路径|动态规划

Total Accepted: 80566 Total Submissions: 225855 Difficulty: Medium A robot is located at the top-left corner of a m&#...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016-03-11 15:16
  • 1590

LeetCode 62 — Unique Paths(C++ Java Python)

题目:http://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x n grid (marked 'Start...
  • dragon_dream
  • dragon_dream
  • 2014-02-23 22:00
  • 2457

LeetCode63——Unique Paths II

LeetCode63——Unique Paths II 跟LeetCode62——Unique Paths不同的是,这里是要在路径上设置障碍的。 因此不能用组合的方法去做了(也许可以,但我实在想不到) 那就是传统二维数组的动态规划了。 定义动归方程如下: 首先 dp[i][j]表示到 点 (i,...
  • zhangxiao93
  • zhangxiao93
  • 2015-11-02 10:01
  • 355

LeetCode63: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...
  • u012501459
  • u012501459
  • 2015-06-12 16:05
  • 878
    个人资料
    • 访问:190471次
    • 积分:4391
    • 等级:
    • 排名:第8123名
    • 原创:245篇
    • 转载:113篇
    • 译文:0篇
    • 评论:30条
    最新评论