zoukankan      html  css  js  c++  java
  • leetcode 62 Unique Paths ---java

    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.

     这道题就是说从start走到finish有多少种走法,只能向右或者向下走。

    刚开始发现超级简单= =。然后发现天真了,这样超时了。

    public class Solution {
        public int uniquePaths(int m, int n) {
            if( m<n)
                return uniquePaths(n,m);
            if( n == 1)
                return 1;
            else
                return uniquePaths(m,n-1)+uniquePaths(m-1,n); 
        }
    }

     然后进行思考,其实是有规律的,就是在n、m均大于1的情况下,其实答案就是C(num1,num2),但是这样的话,在数据比较大的时候会溢出,所以答案也不对。

    public class Solution {
        public int uniquePaths(int m, int n) {
            if( m<n)
                return uniquePaths(n,m);
            if( n == 1)
                return 1;
            int num1 = m+n-2,num2 = n-1;
          int result = 1;
            for( int i = 0;i<num2;i++)
                result*=(num1-i);
            for( int i = 0;i<num2;i++)
                result/=(num2-i);
            return result;
            
            
            
            
            
        }
    }

     利用DP,可以顺利求出结果

    在m>n 并且 n >2 的情况下,

      uniquePaths(m,n) = uniquePaths(m-1,n) + uniquePaths(m,n-1),

    且 uniquePaths(m,n) = uniquePaths(n,m)

    所以可以得到一个二维数组,从而得出答案。

    public class Solution {
        public int uniquePaths(int m, int n) {
            if( m < n)
                return uniquePaths(n,m);
            if( n == 1)
                return 1;
            if( n == 2)
                return m;
            int result[][] = new int[m][m];
            result[0][0] = 1;
            for( int i = 1;i < m ;i++){
                result[i][0] = 1;
                for( int j = 1;j<i;j++){
                    result[i][j] = result[i-1][j]+result[i][j-1];
                }
                result[i][i] = result[i][i-1]*2;
            }
            return result[m-1][n-1];
            
            
            
            
            
            
            
        }
    }

     然后可以继续优化,就是单数组,相当于每次更新之前result数组的每一行。

    public class Solution {
        public int uniquePaths(int m, int n) {
            if( m < n)
                return uniquePaths(n,m);
            if( n == 1)
                return 1;
            if( n == 2)
                return m;
            int result[] = new int[m];
            for( int i = 0;i<m;i++)
                result[i] = 1;
            for( int i = 1;i<n;i++)
                for( int j = 1;j<m;j++)
                    result[j] +=result[j-1];
            return result[m-1];
        
        }
    }
     
  • 相关阅读:
    学习OpenStack之(6):Neutron 深入学习之 OVS + GRE 之 Compute node 篇
    学习OpenStack之(5):在Mac上部署Juno版本OpenStack 四节点环境
    学习OpenStack之 (3):Devstack Screen 使用技巧
    C语言 fread()与fwrite()函数说明与示例
    win C/C++程序通过Get方式获取网页源代码
    ubuntu/wireshark: There are no interfaces on which a capture can be done.故障解决
    [转]VS2010快捷键
    C/C++/Java 程序计时功能函数
    程序设计中使用的那些共享方法或者技术
    GPU/CUDA程序初体验 向量加法
  • 原文地址:https://www.cnblogs.com/xiaoba1203/p/5940550.html
Copyright © 2011-2022 走看看