[LC] 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 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的更多相关文章
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 刷题62. Unique Paths
一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...
- [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 ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 62. Unique Paths
题目: A robot is located at the top-left corner of a m x ngrid (marked 'Start' in the diagram below). ...
- 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 ...
- 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 ...
- 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 ...
- 【一天一道LeetCode】#62. Unique Paths
一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...
随机推荐
- POJ 1860:Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22648 Accepted: 818 ...
- JAVA 算法练习(一)
用java写了几道编程题目,分享给大家 语法和C语言几乎一样,不懂 java 会 c 也可以看明白的. 最大连续数列和 题目说明 对于一个有正有负的整数数组,请找出总和最大的连续数列.给定一个int数 ...
- MVC——EF 回顾总结
回顾一下MVC的知识点. 其实开始 我在学校的知识对MVC 还是很模糊的一个概念.只是记得结合EasyUI 增删改查 和分页,代码都是模糊的 进过这段时间的学习,让我对MVC 有了一个很清楚的认识. ...
- JavaScript学习总结(一)
概述 前端三剑客,html.css.js. 这三种语言基本是前端开发必备的东西,那么你知道这三种语言分别负责的功能是什么吗? html:负责了一个页面的结构 css:负责页面的样式 JavaScrip ...
- 移植sqlite
一.参考文档 1.SQLite安装.编译与应用 2.gcc 生成 .a静态库和 .so动态库 二.下载sqlite 1.sqlite官方首页:https://www.sqlite.org/index. ...
- VuePress 中增加用户登录功能
在 VuePress 中增加用户登录 VuePress 是 Vuejs 官方提供的一个快速建设文档站点的工具,在简单配置好功能后,需要做的事情就剩下写好一个个 Markdown 文档. 因为 VueP ...
- Spring Cloud服务间调用鉴权
学习使用Spring Cloud 微服务间的调用都是RestFul风格,如何保证调用之间的安全性,这是一个很重要的问题. 通过查阅资料http://wiselyman.iteye.com/blog/2 ...
- PAT Advanced 1010 Radix(25) [⼆分法]
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- 如何决定 Web 应用的线程池大小
在部署 web 应用到生产环境,或者在对 web 应用进行性能测试的时候,经常会有人问:如何决定 web 应用线程池大小?决定一个 IO 阻塞型 web 应用的线程池大小是一项很艰巨的任务.通常是通过 ...
- tcp和udp的socket形式
Sockets编程有三种: (1).流套接字(SOCK_STREAM): (2).数据包套接字(SOCK_DGRAM): (3).原始套接字(SOCK_RAW): TCP是流套接字 UCP是数据包套接 ...