Leetcode_62_Unique Paths
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43404205
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.
思路:
(1)题意为给定m行n列,求从第0行第0列走到第m行第n列的路径有多少条。
(2)对于本题,首先想到的是通过递归来实现,当行数为1或者列数为1时,路径只有一条;我们先从行开始,假设从第1行第1列元素开始,向右到达右下角,则可以看做是去除第一列后剩余行列对应路径,以函数f(m,n)表示路径条数,则有f(m,n)=f(1,n-1)+f(2,n-1),...,+f(n,n-1),而f(1,n)=f(m,1)=1,则通过递归即可得到答案,只是递归效率太低,Oj肯定会超时,所以,不能选用这种方法。
(3)考虑到m行n列正好对应一个二位数组,而我们发现f(1,n)=f(m,1)=1,所以,我们对整个二维数组进行拆分,假设拆分第一行,则第一行中任意位置作为终点对应的条数都为1,同理拆分第一列也是;这样,对应二维数组的第一行第一列的值就都为1了;假设数组为2*2,则我们发现到达右下角的路径数为f(2,2)=2=f(2,1)+f(1,2),正好为该位置对应上方和左方值之和;同理,当数组为3*3时,f(3,3)=6=f(3,2)+f(2,3)={f(3,1)+f(2,2)}+{f(1,3)+f{2,2}}={1+f(1,1)+f(1,1)}+{1+f(1,1)+f(1,1)}=6;同理,当数组为m*n时,f(m,n) = f(m-1,n)+f(m,n-1)=.......。所以,我们只需要对二维数组中每个位置遍历赋值即可得到最后的结果,详情见下方代码。
(4)希望本文对你有所帮助。
算法代码实现如下:
/** * @liqq 递归算法能够实现 但是会超时 oj不通过 */ public static int get(int row, int col){ if(row<=0 || col <=0) return 0; if(row==1) return 1; if(col==1) return 1; int result = 0; for (int i = 1; i <=row; i++) { result+=get(i,col-1); } return result; }
/** * @author 二维数组实现 */ public static int getResult(int m, int n){ int[][] arr = new int[m][n]; for (int i = 0; i < m; i++) { arr[i][0]=1; } for (int i = 0; i < n; i++) { arr[0][i]=1; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { arr[i][j]=arr[i-1][j]+arr[i][j-1]; } } return arr[m-1][n-1]; }
Leetcode_62_Unique Paths的更多相关文章
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] 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 : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- 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 Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- soj 1015 Jill's Tour Paths 解题报告
题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...
随机推荐
- 安卓高级5 传感器和震动 模仿微信摇一摇Ui效果
效果图: 所用的Ui就三张图: 案例代码: 结构 MainActivity.java package com.example.myapp; import android.content.Intent; ...
- self关键字
self关键字 self:当前类/对象的指针(指向当前对象/方法调用者) 作用1 当类里有变量名和成员变量名一样的时候,可以使用self区分 例: 我们写一个人的类,有一个年龄属性,在get方法里,我 ...
- Python 表示无穷大的数
我之前只知道设置初始值0.今天偶然在Python算法书上看到这个片段,从100个随机数里面找2个最靠近的自然数(不相等): from random import randrange seq = [ra ...
- 游戏引擎cocos2d-android使用大全
做手机游戏需要三个核心的类,即:SurfaceView,SurfaceHolder,Thread.帧数要在30帧左右是最好的. cocos2d游戏引擎 封装好的框架,可直接使用 cocos2d-and ...
- 悲观的并发策略——Synchronized互斥锁
volatile既然不足以保证数据同步,那么就必须要引入锁来确保.互斥锁是最常见的同步手段,在并发过程中,当多条线程对同一个共享数据竞争时,它保证共享数据同一时刻只能被一条线程使用,其他线程只有等到锁 ...
- FORM触发器
FORM级触发器 PRE-FORM该触发器是在用户双击功能后,进入form前 WHEN-NEW-FORM-INSTANCE该触发器是在用户一进入form时执行 WHEN-FORM-NAVIGAT ...
- 如何查看App provision profile文件中的钥匙链访问组名称
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们因为某些原因希望安全的在多个App中共享一些信息,我们可以 ...
- 自定义gradview
http://blog.csdn.net/jdsjlzx/article/details/7525724 虽然Android已自带了GridView,但是,却不够灵活,同时也不能自由添加控件,因此,本 ...
- 1.3、Android Studio创建一个Android Library
一个Android Library结构上与Android app模块相同.它可以包含构建一个app需要的所有东西,包括圆满,资源文件和AndroidManifest.xml.然而,并非编译成运行在设备 ...
- shell脚本实现冒泡排序
手动输入一行字符串,并对其排序. 脚本如下: #!/bin/bash #a test about sort echo "please input a number list" re ...