leetCode 62.Unique Paths (唯一路径) 解题思路和方法
2015-07-15 14:23
405 查看
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://leetcode.com/wp-content/uploads/2014/12/robot_maze.png)
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
思路:这题首先想到的是递归,但是递归效率太慢,超时不解释。于是换动态规划,也算典型的动态规划的题了。
具体代码如下:
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://leetcode.com/wp-content/uploads/2014/12/robot_maze.png)
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
思路:这题首先想到的是递归,但是递归效率太慢,超时不解释。于是换动态规划,也算典型的动态规划的题了。
具体代码如下:
public class Solution { public int uniquePaths(int m, int n) { //本题解法为动态规划 //状态转移方程f[i][j] = f[i-1][j] + f[i][j-1]; //f[i][j]的值即为路径的数量 int[][] f = new int[m] ; for(int i = 0; i < m ; i++)//第一列赋值为1 f[i][0] = 1; for(int i = 0; i < n; i++)//第一行赋值为1 f[0][i] = 1; for(int i = 1; i < m ; i++) for(int j = 1; j < n ; j++){ f[i][j] = f[i-1][j] + f[i][j-1]; //System.out.println("f[" + i +"][" + j+ "]" + f[i][j]); } return f[m-1][n-1];//返回结果值 } }
相关文章推荐
- UITextView 响应 键盘的return(完成键)
- leetCode 60.Permutation Sequence (排列序列) 解题思路和方法
- 使用uiautomator获取应用内存
- 【原】设置iOS项目BuildVersion自动增加
- UIimageview tap 跳转
- iOS开发——UI基础-自定义构造方法,layoutSubviews,Xib文件,利用Xib自定义View
- 如何编译WinCE Bluetooth Stack并打开的默认LOG
- Forbidden (403) CSRF verification failed. Request aborted.
- UEFI HOB
- UEFI Drivers & UEFI Driver Model
- UEFI Images
- UEFI小结-Handle的来龙去脉
- UEFI Boot Flow
- EasyUI改变Layout的Region的宽高,位置等信息
- Android UI学习之---Button
- UI-程序的运行顺序
- SQL Server 阻止了对组件 \'Ad Hoc Distributed Queries\' 的访问
- [CareerCup] 1.1 Unique Characters of a String 字符串中不同的字符
- iOS开发——UI基础-懒加载,plist文件,字典转模型,自定义view
- Ubuntu——grub rescue 主引导修复