leetcode-62. Unique Paths
来源:互联网 发布:动态截图软件 编辑:程序博客网 时间:2024/06/10 02:28
https://leetcode.com/problems/unique-paths/#/solutions
问题描述:有一个m*n的矩阵,从左上角走到右下角,每次只能走一步,(要么向右走,要么向下走),问有多少中走法。
思路解析:DP
建立一个数组g[][]用来存储走到每个位置的步数,
假如,从中间取一个位置,该位置一定是有他的左面和上面过来的,所以他的步数 g[i][j]=g[i-1][j]+g[i][j-1];但是还需要考虑边界,因为在矩阵的周围比如说最顶端,只能从左到右走1种走法。
代码如下:
public class Solution { public int uniquePaths(int m, int n) { int[][] g=new int[m][n]; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(i==0 || j==0) { g[i][j]=1; }else { g[i][j]=g[i-1][j]+g[i][j-1]; } } } return g[m-1][n-1]; }}
阅读全文
0 0
- [LeetCode]62.Unique Paths
- LeetCode --- 62. Unique Paths
- LeetCode 62.Unique Paths
- [Leetcode] 62. Unique Paths
- [leetcode] 62.Unique Paths
- [leetcode] 62.Unique Paths
- leetcode 62. Unique Paths
- Leetcode 62. Unique Paths
- [leetcode] 62. Unique Paths
- 62. Unique Paths LeetCode
- [LeetCode]62. Unique Paths
- 【LeetCOde】62. Unique Paths
- [LeetCode]62. Unique Paths
- leetcode 62. Unique Paths
- LeetCode 62. Unique Paths
- LeetCode *** 62. Unique Paths
- leetcode 62. Unique Paths
- Leetcode:62. Unique Paths
- 图解NIO(1) Buffer 与 Channel
- opencv(24)---轮廓特征属性及应用之最小外接圆
- 《lua程序设计》读书笔记 第二章:类型与值
- 关于select2默认选中 动态选中 显示,刷新选中
- 数据库—(5)
- leetcode-62. Unique Paths
- jquery-validate的使用(一)
- 返回星期方法
- 开发
- 浅析C#中的Attribute
- 安装intel compiler and mkl library
- POJ 3680 Intervals 区间k覆盖 费用流
- vue muit-ui infinite-scroll源码解析
- PHP单例模式