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];
}
}

最新文章

  1. CSS学习笔记2-2d变换和过渡属性
  2. WVS简单使用
  3. 基础篇-Windows保护模式
  4. 【转】关于 hashCode() 你需要了解的 3 件事
  5. PL/SQL中批量执行SQL脚本(不可把所有的语句都复制到New SQL Windows)
  6. php laravel 安装
  7. ecmall数据库基本操作
  8. 【转】Notepad++ 快捷键 大全 官方整理过来的
  9. python 入门快速学习整理
  10. 【实战】静默安装-oracle 11.2.0.3 on centos 5.10
  11. react视频入门
  12. 【HotSpot】jps命令行详解
  13. 第五章 MySQL事务,视图,索引,备份和恢复
  14. for、for in和while以及do while
  15. Java web的一些面试题
  16. 【Teradata SQL】创建数据库和表
  17. 【Mongo】安装并配置副本集
  18. PostMan打不开怎么解决
  19. -bash: xhost: command not found
  20. perventDefault, stopPropagation, stopImmediatePropagation 三者的区别

热门文章

  1. COCOS2D-X多层单点触摸分发处理方案?
  2. 《linux系统及其编程》实验课记录(四)
  3. 转:解决Python2.7的UnicodeEncodeError: ‘ascii’ codec can’t encode异常错误
  4. 终端利用ssh登录远程服务器
  5. WPF 渲染级别 (Tier)
  6. 第九讲 C#练习题
  7. 【BZOJ4726】[POI2017]Sabota? 树形DP
  8. ios mrc &amp; arc 并用
  9. 简述泛型、用Maven创建Web项目以及在Web项目上整合SpringMVC
  10. JAVA基础之JDBC开发、JSTL语法、EL表达式与数据分页