1. /**
  2. * Created by lvhao on 2017/7/6.
  3. * A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
  4.  
  5. The robot can only move either down or right at any point in time.
  6. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
  7.  
  8. How many possible unique paths are there?
  9. 动态规划,目标是到最终点的路径有几条。每个点的路径都是能到这个点的上一个点的路径之和,由于
  10. 只能向下和向右走,所以动态方程是A[i][j] = A[i][j-1] + A[j][j+1]
  11. 算出每个点的值就行,最后返回A[m-1][n-1]
  12. */
  13. public class Q62UniquePaths {
  14. public static void main(String[] args) {
  15. System.out.println(uniquePaths(3,3));
  16. }
  17. public static int uniquePaths(int m, int n) {
  18. if (m == 1 || n == 1)
  19. return 1;
  20. int[][] res = new int[m][n];
  21. //一开始要先把二维数组上和左边的初始化为1,这是初始条件
  22. for (int i = 0; i < m; i++) {
  23. res[i][0] = 1;
  24. }
  25. for (int i = 0; i < n; i++) {
  26. res[0][i] = 1;
  27. }
  28. //动态规划
  29. for (int i = 1; i < m; i++) {
  30. for (int j = 1;j < n;j++)
  31. {
  32. res[i][j] = res[i-1][j] + res[i][j-1];
  33. }
  34.  
  35. }
  36. return res[m-1][n-1];
  37. }
  38.  
  39. }

[leetcode]62.UniquePaths的更多相关文章

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

  2. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  3. LeetCode 62,从动态规划想到更好的解法

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第36篇文章,我们一起来看下LeetCode的62题,Unique Paths. 题意 其实这是一道老掉牙的题目了 ...

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

  5. leetcode 62 不同的路径

    描述: m*n的矩阵,从左上角走到右下角,只能向下或向右走. 解决: 简单dp,dp[i][j]表示到i,j这点总共多少种路径. dp[i][j] = dp[i][j - 1] + dp[i - 1] ...

  6. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...

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

  8. LeetCode.62——不同路径

    问题描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为 ...

  9. Java实现 LeetCode 62 不同路径

    62. 不同路径 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中 ...

随机推荐

  1. 我与PHP,ULM和Vue.js不得不说的故事(一个放荡不羁与一个神神秘秘一个似曾相识,从入门到放弃记录第二章)

    ·关于UML(git) 究竟是命运在茫茫语言之中遇到了你,还是我的魅力让你向我奔涌而来.好吧都不是,我俩就像古代包办婚姻,被专业牢牢的绑在一起了,既然都是一条绳上的蚂蚱.我我们应该能体谅彼此的不容易, ...

  2. Fiddler 4 断点调试(修改response请求参数)

    1.选择测试链接 2. 2然后点击规则的Automatic Breakpoints 的 After Responses 3.然后重新发起请求并找到链接 4.然后修改数据 5.最终效果

  3. 第14.4节 使用IE浏览器获取网站访问的http信息

    上节<第14.3节 使用google浏览器获取网站访问的http信息>中介绍了使用Google浏览器怎么获取网站访问的http相关报文信息,本节介绍IE浏览器中怎么获取相关信息.以上节为基 ...

  4. Ubuntu 16.04添加阿里云源

    转自:http://www.cnblogs.com/EasonJim/p/7119156.html 添加国内源有个好处,比如下载软件时直接时国内的服务器,速度有保证. 以下是操作方法: 1.备份 su ...

  5. 网鼎杯 fakebook

    这道题目登录之后我们可以看到有join和login login即登录,join即注册 我们通过查看robots.txt可以知道 有源代码泄露. 先将泄露的源码下载下来审计一波 <?php cla ...

  6. 总结下flask中的宏、Jinjia2语法

    这几天学的东西比较多,时间又有点不够用,趁着快吃饭了,赶紧总结总结. 00x1 宏: 如果学过C语言的童鞋,可能知道宏在C语言里面是一个定义一个固定参数的变量.在flask里面,宏是相当于一个函数的作 ...

  7. 二分查找——没有想象中的容易(详解各种变式,超深度理解,c++)

    int binarySearch(int[] nums, int target) { int left = 0; int right = nums.length - 1; // 注意 while(le ...

  8. window下使用cmd查看端口占用的进程,并杀死该进程

    做项目的时候经常会遇到"address already in use"的情况,此时可以选择使用dos命令将该进程杀死. 首先,查找端口对应的进程,使用命令(以进程号8080为例): ...

  9. git 常用命令--超实用

    git命令行常用操作 1.配置ssh key git config --global user.name 'git用户名' git config --global user.email '邮箱地址' ...

  10. linux 配置本地yum源,配置国内yum源,配置epel源

    目录 一.配置本地yum源 二.配置国内yum源和epel源 一.配置本地yum源 1.挂载ISO镜像 mount -o loop /mnt/yum-iso/CentOS-7-x86_64-DVD-1 ...