---
title: "LeetCode-62-Unique Paths*(动态规划) "
author: "随便小屋"
date: "2015-08-29T20:22:23Z"
tags: ["Array"]
categories: ["编程语言"]
slug: "leetcode-62-unique-paths"
draft: false
lastmod: "2015-08-29T20:24:10Z"
id: 2238927
url: "https://logicluo.iteye.com/blog/2238927"
---

Unique Paths

 

来自 &lt;[https://leetcode.com/problems/unique-paths/](https://leetcode.com/problems/unique-paths/)&gt;

 

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?

![](http://dl2.iteye.com/upload/attachment/0111/3090/744fa2d6-290e-3dd2-86b4-1a06311e575b.png)  
 

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

题目解读：

一个机器人在二维m x n网格的最左上方(在下面的例子中标识为‘Start’)。这个机器人只能向右或者向下移动，它想到达二维网格的最右下方（在下面的例子中标识为‘Finish’）。

一共有多少种不同的路径？

![](http://dl2.iteye.com/upload/attachment/0111/3086/42c57dc2-ed61-321b-bb0c-bd885a97c586.png)  
 

Note:

m和n最大为100.

 

解析：

这是一个动态规划的题，如果把任意网格作为最终目的地。则到达其位置的不同路径种数为到其上方和左方不同路径种数之和。

 

 

Java代码：

```
public class Solution {
    	//动态规划
	public int uniquePaths(int m, int n) {
		if(0==m || 0==n) 
			return 0;
		//一行一列只能有一种情况
		if(1==m && 1==n)
			return 1;
		
		int[][] dynamicPlanning = new int[m][n];
		
		//给最左端一行进行赋值1，标识从开始点到左端的任意一个地方只有一种办法
		for(int i=0; i<m; i++)
			dynamicPlanning[i][0] = 1;
		//给最上放一行赋值1，表示从开始点到上端任意一个地方只有一种方法
		for(int i=0; i<n; i++)
			dynamicPlanning[0][i] = 1;
		
		for(int i=1; i<m; i++) {
			for(int j=1; j<n; j++) 
				//到达二维数组中任意一点的总和为其位置上方和左方元素之和
				dynamicPlanning[i][j] = dynamicPlanning[i-1][j] + dynamicPlanning[i][j-1];
		}
		return dynamicPlanning[m-1][n-1];
	}
}
```

 

算法行能：

![](http://dl2.iteye.com/upload/attachment/0111/3088/9514bcbe-bdc5-38eb-8bca-bc85755b39c7.png)