[LeetCode116]Unique Paths
来源:互联网 发布:刘弘章 知乎 编辑:程序博客网 时间:2024/05/20 03:47
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.
Analysis:
DP, transction function is DP[i][j] = DP[i-1][j]+DP[i][j-1];
this could be implemented in one dimension DP
java
public int uniquePaths(int m, int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(m==0 || n==0) return 0; int [][] dp = new int [m][n]; for(int i=0; i<m; i++){ dp[i][0]=1; for(int j=1;j<n;j++){ if(i==0) dp[i][j] = 1; else dp[i][j] = dp[i-1][j] + dp[i][j-1]; } } return dp[m-1][n-1]; }s2:
public int uniquePaths(int m, int n) { if(m==0 || n==0) return 0;int []dp = new int[n];dp[0]=1;for(int i=0;i<m;i++){for(int j=1;j<n;j++)dp[j] = dp[j-1]+dp[j];}return dp[n-1]; }c++
int uniquePaths(int m, int n) { vector <int> maxpath(n,0); maxpath[0] = 1; for(int i=0; i<m;i++){ for(int j=1; j<n;j++){ maxpath[j] = maxpath[j-1]+maxpath[j]; } } return maxpath[n-1];}
0 0
- [LeetCode116]Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- Unique Paths
- 《Python核心编程》第8章 条件和循环 练习
- Ubuntu recovery模式下修改回密码
- android.intent.action.MAIN与android.intent.category.LAUNCHER
- 计算相对路径
- AtomicInteger
- [LeetCode116]Unique Paths
- 编程中的命名设计那点事
- JDK1.5 泛型
- eclipse 增加cpp 语法高亮插件
- PHP 中获取文件名及路径
- 实例学习maven 项目构建 .
- Using FFTW (the fastest FFT) under Visual Studio 2013
- 青涩记忆
- 两个有序的数组,挑选出相同的元素