本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  2. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. [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 ...

  4. leetcode : Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  5. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  6. 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 ...

  7. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  8. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  9. soj 1015 Jill's Tour Paths 解题报告

    题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...

随机推荐

  1. 一个未排序整数数组,有正负数,重新排列使负数排在正数前面,并且要求不改变原来的正负数之间相对顺序,比如: input: 1,7,-5,9,-12,15 ans: -5,-12,1,7,9,15 要求时

    #include <iostream> using namespace std; void txsort(int* arr, int len) { if (!arr || len == 1 ...

  2. gloox配置聊天室

    gloox配置聊天室 (金庆的专栏) gloox是XMPP协议的C++客户端库.以下代码测试创建多人聊天室(MUC), 并进行配置.参照gloox中的muc示例代码.gloox代码示例中没有聊天室的配 ...

  3. Swift中switch强大的模式匹配

    不少人觉得Swift中switch语句和C或C++,乃至ObjC中的差不多,此言大谬! 让本猫带领大家看一下Swift中switch语句模式匹配的威力. 所谓模式匹配就是利用一定模式(比如couple ...

  4. Github上的Android项目介绍之ListViewAnimation(针对listView item的侧滑菜单)(1)

    demo源码,需要可以下载 1.这是一个github开源项目,先去github上面下载,github下载地址. 2.将SwipeMenuListView项目,导入,然后新建项目如果要引用,要设置为相应 ...

  5. 使用Apache的ab进行压力测试

    概述 ab是apache自带的压力测试工具,当安装完apache的时候,就可以在bin下面找到ab然后进行apache 负载压力测试. 后台测试开发中,常用的压力测试服务,php一般选择xampp,下 ...

  6. 求链表倒数第n个元素

    提示:设置一前一后两个指针,一个指针步长为1,另一个指针步长为n,当一个指针走到链表尾端时, 另一指针指向的元素即为链表倒数第n个元素. #include <stdio.h> #inclu ...

  7. K均值聚类的失效性分析

    K均值聚类是一种应用广泛的聚类技术,特别是它不依赖于任何对数据所做的假设,比如说,给定一个数据集合及对应的类数目,就可以运用K均值方法,通过最小化均方误差,来进行聚类分析. 因此,K均值实际上是一个最 ...

  8. 带你深入理解STL之List容器

    上一篇博客中介绍的vector和数组类似,它拥有一段连续的内存空间,并且起始地址不变,很好的支持了随机存取,但由于是连续空间,所以在中间进行插入.删除等操作时都造成了内存块的拷贝和移动,另外在内存空间 ...

  9. Erlang 集群互连测试

    Erlang 集群互连测试Erlang节点相同cookie全互联成为一个集群(cluster).如果2个集群不同cookie, 然后其中有节点连接到对方集群的节点,这2个集群会合并成一个集群吗?连接到 ...

  10. Linux内核分配内存的方式

    page = alloc_pages(GFP_KERNEL, get_order(1234));分配失败返回NULLGFP_KERNEL  ---> 分配标志,当没有足够内存分配时,睡眠阻塞,直 ...