leetcode oj java Unique Paths

来源:互联网 发布:tensorflow viterbi 编辑:程序博客网 时间:2024/05/16 19:09

一、问题描述:

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很容易想到递归: f(m,n) = f(m-1,n) + f(m,n-1)  , 终止条件是m==1 || n==1但是time limit

2动态规划,: 用二维数组存放路径数目, a[m][n] = a[m-1][n] + a[m][n-1]


三、代码:

package T12;/** * @author 作者 : xcy * @version 创建时间:2016年12月20日 下午3:42:16 *          类说明 */public class t62 {    public static int re = 0;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println(uniquePaths(10, 10));    }    //    public static int uniquePaths(int m, int n) {    //        if (m == 1 || n == 1) {    //            return 1;    //        }    //        return uniquePaths(m - 1, n) + uniquePaths(m, n - 1);    //    }    public static int uniquePaths(int m, int n) {        int[][] re = new int[m][n];        for (int i = 0; i < m; i++) {            re[i][0] = 1;        }        for (int j = 0; j < n; j++) {            re[0][j] = 1;        }        for (int i = 1; i < m; i++) {            for (int j = 1; j < n; j++) {                re[i][j] = re[i - 1][j] + re[i][j - 1];            }        }        return re[m - 1][n - 1];    }}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果电量用的快怎么办 苹果5s掉电快怎么办 小米5s返回键失灵怎么办 苹果5s漏电是怎么办 苹果6s手机漏电怎么办 5s返回键失灵怎么办 苹果5s返回键失灵怎么办 小米5返回键失灵怎么办 小米手机返回键失灵怎么办 萍果6splus关机键失灵怎么办 苹果5s很快没电怎么办 天猫分期付款没有花呗怎么办 淘宝花呗分期额度不够怎么办 支付宝余额支付超限怎么办 想分期花呗不够怎么办 来分期提现不了怎么办 帮朋友分期手机他不还钱怎么办 手机分期套现被骗了怎么办 学生既有分期办手机被骗怎么办? 京东刚买完东西第二天降价了怎么办 微信上买东西不给退货怎么办 微信买东西转红包被骗怎么办 买手机分期被骗了怎么办 苹果手机摔弯了怎么办 6s主板摔弯了怎么办 京东显示器坏了怎么办 苹果6s手机弯了怎么办 苹果手机变弯了怎么办 苹果手机屏幕摔碎了怎么办 苹果6p后壳弯曲怎么办 苹果手机后壳有点弯曲怎么办 苹果手机x弯了怎么办 苹果8手机弯了怎么办 苹果8p手机弯了怎么办? 美版iphone弯了怎么办 oppor9手机听筒声音小怎么办 苹果7摔弯了屏幕怎么办 苹果手机压弯了怎么办 京东买的电脑坏了怎么办 拼多多低价乱价怎么办 厂家不通过经销商直接发货怎么办