LeetCode 62. Unique Paths(所有不同的路径)
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.
题目标签:Array
这道题目给了我们一个matrix 的m 和n, 让我们从[0,0] 开始,找到所有不同的路径到[m-1][n-1]。我们可以来看一个比原题简单一点的例子:
3 * 3 matrix,我们设起点为1。每一个点代表的是,走到这个点的不同路径的数量
1 0 0
0 0 0
0 0 0
我们可以发现,机器人只能往右和下走,所以路径的可能只能来自于左和上。
所以我们可以遍历matrix,把这个点的左边点,和上面点加起来,就等于到这个点的路径之和。
1 1 1
1 2 3
1 3 6
最后一个点,[m-1][n-1]就是所有不同路径的总数。
Java Solution:
Runtime beats 5.14%
完成日期:07/20/2017
关键词:Array
关键点:Dynamic Programming, 逆向思考
public class Solution
{
public int uniquePaths(int m, int n)
{
int[][] matrix = new int[m][n]; matrix[0][0] = 1; // start point for(int i=0; i<m; i++) // row
{
for(int j=0; j<n; j++) // column
{
// get left
if(j-1 >=0)
matrix[i][j] += matrix[i][j-1];
// get top
if(i-1 >=0)
matrix[i][j] += matrix[i-1][j];
}
} return matrix[m-1][n-1];
}
}
参考资料:
http://www.cnblogs.com/grandyang/p/4353555.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 62. Unique Paths(所有不同的路径)的更多相关文章
- [LeetCode] 62. Unique Paths 唯一路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- [LeetCode] 62. Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- LeetCode 62. Unique Paths不同路径 (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [leetcode]62. Unique Paths 不同路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 63. Unique Paths II 不同的路径之二
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [leetcode] 62 Unique Paths (Medium)
原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
随机推荐
- linux文件截取前几行,后几行,中间几行命令
1. 如果你只想看文件的前5行,可以使用head命令,如: head -5 /etc/passwd 2. 如果你想查看文件的后10行,可以使用tail命令,如: tail -2 /etc/passwd ...
- 关于APP分享到QQ、微信等
<script> var shares=null; var Intent=null,File=null,Uri=null,main=null; function plusRe ...
- eclipse Maven新建一个项目并使用
安装参考这篇博文eclipse配置maven + 创建maven项目(三) 打开pom.xml 试着添加MySQL的JDBC驱动 添加如下配置, <dependency> <g ...
- [python学习笔记] String格式化
格式化 S % (args...) 方式 特点 str里的占位符同java里的占位符. 优势 这种方式可以限定格式化的时候接受的数据类型. 常见占位符 %d 接收数字,格式化为 十进制 %x 接收数字 ...
- 杂谈--DML触发器学习
触发器按类型分为三类: 1. DML 触发器,在数据变更时触发: 2. DDL 触发器,在修改数据库级别或实例级别对象时触发: 3. Login 触发器,在用户登录时触发: 最常见的是DML触发器,D ...
- 【转】独立游戏如何对接STEAM SDK
独立开发者在对接STEAM SDK之前 首先得先登上青睐之光,也就是我们俗称的"绿光" 一般要先对接G胖家的SDK,然后提交版本,最后等待审核... 我本身是unity 开发,对C ...
- UI自动化测试(二)浏览器操作及对元素的定位方法(xpath定位和css定位详解)
Selenium下的Webdriver工具支持FireFox(geckodriver). IE(InternetExplorerDriver).Chrome(ChromeDriver). Opera( ...
- Python文件复制(txt文件)
功能:这个py脚本是把一个txt文件(源文件)复制到另一个txt文件(目的文件)里面 算法思路: 程序首先判断源文件(用exists函数判断)和目的文件是否存在,如果不存在则输出文件路径不存在,如果存 ...
- Element-ui Theme浅析
一.浅析 1.采用BEM方式管理类名 B:block,模块,一个块是一个独立的实体,块可以包含其它块,名字单词间用-连接:如一个搜索块: E:element,元素,一个元素是块的一部分,具有某种功能, ...
- 冒泡排序(Bubble Sort)
冒泡排序的基本思路 冒泡排序是一种效率极低的排序,首先它需要知道数组的有效数据长度,再对数据第一个和第二个两两比较,按照比较规则进行交换,然后第二个数据和第三个数据进行比较,按照比较规则进行交换:第一 ...