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?

Solution:

Space: O(M * N)

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

Optimized Space to O(N)

class Solution {
public int uniquePaths(int m, int n) {
if (m == 0 || n == 0) {
return 0;
}
if (m == 1 ||n == 1) {
return 1;
}
int [] paths = new int[n];
Arrays.fill(paths, 1);
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
paths[j] += paths[j - 1];
}
}
return paths[n - 1];
}
}

[LC] 62. Unique Paths的更多相关文章

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

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

  2. 刷题62. Unique Paths

    一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...

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

  4. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  5. 62. Unique Paths

    题目: A robot is located at the top-left corner of a m x ngrid (marked 'Start' in the diagram below). ...

  6. LeetCode OJ 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 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. 62. Unique Paths(中等,我自己解出的第一道 DP 题^^)

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. 【一天一道LeetCode】#62. Unique Paths

    一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...

随机推荐

  1. Android 公告新闻消息资讯之垂直滚动效果

    垂直滚动新闻栏的实现原理: 就是一个自定义的LinearLayout,并且textView能够循环垂直滚动,而且条目可以点击,显示区域最多显示2个条目,并且还有交替的属性垂直移动的动画效果,通过线程来 ...

  2. Ubuntu系统的软件源更换

    参考:https://www.daweibro.com/node/142 什么是Ubuntu的软件源? 我们在使用Debian或者Ubuntu的apt-get工具来安装需要的软件时,其实就是从服务器获 ...

  3. 移植sqlite

    一.参考文档 1.SQLite安装.编译与应用 2.gcc 生成 .a静态库和 .so动态库 二.下载sqlite 1.sqlite官方首页:https://www.sqlite.org/index. ...

  4. 吴裕雄--天生自然深度学习TensorBoard可视化:projector_data_prepare

    import os import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from tensorflow ...

  5. My97DatePicker日历插件

    My97DatePicker具有强大的日期功能,能限制日期范围,对于编写双日历比较简便. 注意事项: My97DatePicker目录是一个整体,不可以破坏 My97DatePicker.html 是 ...

  6. tensorflow object detection api android

    https://blog.csdn.net/weixin_40355324/article/details/80651350

  7. Graph & Tree

    图论学习笔记 TYQ图论真是个渣渣呢 所以TYQ决定猛补图论 好的从0x60开始 表示博客园不用Latex真的烦呢QAQ,公式难打的要命QAQ 0x60~0x62 最短路讲解跳过 最小生成树: Kru ...

  8. python实现图书管理系统

    # 用户注册 def logon(): print("欢迎来到图书管理系统注册页面~") username = input("请输入用户名:") if len( ...

  9. 给c盘瘦身

    火狐浏览器缓存 C:\Users\lenovo\AppData\Local\Mozilla\Firefox\Profiles\5nk022sw.default\cache2\entries ‪C:\U ...

  10. dubbo的本地存根

    在消费者创建存根类 修改消费者XML 也可以修改消费者注解