62. Unique Paths (走棋盘多少种不同的走法 动态规划)
2024-08-31 05:41:43
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.
r如果用公式的话 就是 C(X+Y 选X)
class Solution {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n]; for(int i =0;i<m;i++){
for (int j = 0;j < n;j++){
if(i==0&&j==0)
dp[i][j]=1;
else if(i==0)
dp[i][j] = dp[i][j-1];
else if(j==0)
dp[i][j] = dp[i-1][j];
else
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}
}
class Solution {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];
for(int i = 0;i<m;i++)
dp[i][0] = 1;
for(int i = 0;i<n;i++)
dp[0][i] = 1;
for(int i =1;i<m;i++)
for (int j = 1;j < n;j++)
dp[i][j] = dp[i-1][j] + dp[i][j-1];
return dp[m-1][n-1];
}
}
最新文章
- CSS学习笔记2-2d变换和过渡属性
- WVS简单使用
- 基础篇-Windows保护模式
- 【转】关于 hashCode() 你需要了解的 3 件事
- PL/SQL中批量执行SQL脚本(不可把所有的语句都复制到New SQL Windows)
- php laravel 安装
- ecmall数据库基本操作
- 【转】Notepad++ 快捷键 大全 官方整理过来的
- python 入门快速学习整理
- 【实战】静默安装-oracle 11.2.0.3 on centos 5.10
- react视频入门
- 【HotSpot】jps命令行详解
- 第五章 MySQL事务,视图,索引,备份和恢复
- for、for in和while以及do while
- Java web的一些面试题
- 【Teradata SQL】创建数据库和表
- 【Mongo】安装并配置副本集
- PostMan打不开怎么解决
- -bash: xhost: command not found
- perventDefault, stopPropagation, stopImmediatePropagation 三者的区别
热门文章
- COCOS2D-X多层单点触摸分发处理方案?
- 《linux系统及其编程》实验课记录(四)
- 转:解决Python2.7的UnicodeEncodeError: ‘ascii’ codec can’t encode异常错误
- 终端利用ssh登录远程服务器
- WPF 渲染级别 (Tier)
- 第九讲 C#练习题
- 【BZOJ4726】[POI2017]Sabota? 树形DP
- ios mrc &; arc 并用
- 简述泛型、用Maven创建Web项目以及在Web项目上整合SpringMVC
- JAVA基础之JDBC开发、JSTL语法、EL表达式与数据分页