[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 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 7 x 3 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
Example 1:
Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right
Example 2:
Input: m = 7, n = 3
Output: 28
解题思路:
class Solution {
public int uniquePaths(int m, int n) {
if (m == 0 || n == 0) {
return 1;
} int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) {
dp[i][0] = 1;
}
for (int i = 0; i < n; i++) {
dp[0][i] = 1;
} for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
}
Java Solution 2:
class Solution {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];
int i, j;
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++ j) {
if (i == 0 || j == 0) {
dp[i][j] = 1;
}
else {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
}
return dp[m-1][n-1];
}
}
CPP:
class Solution {
public:
/**
* @param n, m: positive integer (1 <= n ,m <= 100)
* @return an integer
*/
int uniquePaths(int m, int n) {
// wirte your code here
vector<vector<int> > f(m, vector<int>(n)); for(int i = 0; i < n; i++)
f[0][i] = 1; for(int i = 0; i < m; i++)
f[i][0] = 1; for(int i = 1; i < m; i++)
for(int j = 1; j < n; j++)
f[i][j] = f[i-1][j] + f[i][j-1]; return f[m-1][n-1];
}
};
Python:
class Solution(object):
def uniquePaths(self, m, n):
dp = [[0] * n for i in xrange(m)]
for i in xrange(m):
for j in xrange(n):
if i == 0 or j == 0:
dp[i][j] = 1
else:
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] return dp[m -1][n - 1]
Python: Time: O(m * n) Space: O(m + n)
class Solution:
# @return an integer
def uniquePaths(self, m, n):
if m < n:
return self.uniquePaths(n, m)
ways = [1] * n for i in xrange(1, m):
for j in xrange(1, n):
ways[j] += ways[j - 1] return ways[n - 1]
Python:
class Solution:
# @return an integer
def c(self, m, n):
mp = {}
for i in range(m):
for j in range(n):
if(i == 0 or j == 0):
mp[(i, j)] = 1
else:
mp[(i, j)] = mp[(i - 1, j)] + mp[(i, j - 1)]
return mp[(m - 1, n - 1)] def uniquePaths(self, m, n):
return self.c(m, n)
Python: wo
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
dp = [[0] * n for i in xrange(m)] # m, n不能反了
for i in xrange(m):
for j in xrange(n):
if i == 0 and j == 0:
dp[i][j] = 1
elif i == 0:
dp[i][j] = dp[i][j-1]
elif j == 0:
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = dp[i-1][j] + dp[i][j-1] return dp[-1][-1]
JavaScript:
/**
* @param m: positive integer (1 <= m <= 100)
* @param n: positive integer (1 <= n <= 100)
* @return: An integer
*/
const uniquePaths = function (m, n) {
var f, i, j;
f = new Array(m);
for (i = 0; i < m; i++) f[i] = new Array(n);
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (i === 0 || j === 0) {
f[i][j] = 1;
} else {
f[i][j] = f[i - 1][j] + f[i][j - 1];
}
}
}
return f[m - 1][n - 1];
}
[LeetCode] 62. Unique Paths 唯一路径的更多相关文章
- LeetCode 62. Unique Paths不同路径 (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [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 ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- [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 ...
- 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 ...
- [leetcode] 62 Unique Paths (Medium)
原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- 62. Unique Paths不同路径
网址:https://leetcode.com/problems/unique-paths/ 第一思路是动态规划 通过观察,每一个格子的路线数等于相邻的左方格子的路线数加上上方格子的路线数 于是我们就 ...
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
随机推荐
- 解决 Vue 刷新页面后 store 数据丢失的问题
原来的状态(页面刷新数据会重置) state: { teamA: '主队' }, mutations: { data_teamA(state, x) { state.teamA = x } }, ...
- moviepy草码
第一下. # coding=utf-8 from moviepy.editor import * from moviepy.video.tools.subtitles import Subtitles ...
- mysql - Centos安装MySQL
环境:Centos7.3 No.1 切换为root用户,是则略过 su root No.2 下载MySQL的repo源 wget http://repo.mysql.com/mysql-communi ...
- Spark API--Spark 分区
一.分区的概念 分区是RDD内部并行计算的一个计算单元,RDD的数据集在逻辑上被划分为多个分片,每一个分片称为分区,分区的格式决定了并行计算的粒度,而每个分区的数值计算都是在一个任务中进行的,因此任务 ...
- 使用postman上传excel文件测试导入excel
今日思语:城市的生活很快,有时学会让自己慢下来,慢慢来 对于做一些文件上传操作时,一般我们是直接在前端页面加入类型为file的input标签,也可以使用postman来进行文件的上传测试,如下: po ...
- 09-Flutter移动电商实战-移动商城数据请求实战
1.URL接口管理文件建立 第一步需要在建立一个URL的管理文件,因为课程的接口会一直进行变化,所以单独拿出来会非常方便变化接口.当然工作中的URL管理也是需要这样配置的,以为我们会不断的切换好几个服 ...
- LeetCode 855. Exam Room
原题链接在这里:https://leetcode.com/problems/exam-room/ 题目: In an exam room, there are N seats in a single ...
- LeetCode 988. Smallest String Starting From Leaf
原题链接在这里:https://leetcode.com/problems/smallest-string-starting-from-leaf/ 题目: Given the root of a bi ...
- 在x64计算机上捕获32位进程的内存转储
这是一个我经常遇到的问题,我们经常会遇到这样的情况:我们必须重新捕获内存转储,因为内存转储是以“错误”的方式捕获的.简而言之:如果在64位计算机上执行32位进程,则需要使用允许创建32位转储的工具捕获 ...
- 洛谷 P5506 封锁
目录 题目 思路 \(Code\) 题目 P5506 封锁 思路 模拟 \(\large\text{读题一定要细心}\) 解释都在代码里. \(Code\) #include<bits/stdc ...