35.Unique Paths(不同的路径)
Level:
Medium
题目描述:
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
思路分析:
题目要求求出一个机器人从矩阵的左上角走到矩阵的右下角,一共有多少种走法。可以用动态规划的思想来解决这道题,我们用dp[ i ] [ j ]来表示走到第i行和第j列,共有多少种走法。由题意知,机器人只能向右和向下走,那么状态转移方程是 dp[ i ] [ j ]=dp [i-1] [ j ]+dp[ i ] [ j-1]。注意到矩阵第一行或者第一列某个位置,路径只有一条。(因为起点是左上角,并且只能向右向下移动)
代码:
public class Solution{
public int uniquePaths(int m,int n){
int [][]dp=new int [m][n];
for(int i=0;i<m;i++){
dp[i][0]=1;
}
for(int j=0;j<n;j++){
dp[0][j]=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];
}
}
35.Unique Paths(不同的路径)的更多相关文章
- [LeetCode] 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每天一题】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 OJ: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 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). ...
- [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] unique paths ii 独特路径
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 063 Unique Paths II 不同路径 II
这是“不同路径” 的进阶问题:现在考虑网格中有障碍物.那样将会有多少条不同的路径从左上角到右下角?网格中的障碍物和空位置分别用 1 和 0 来表示.例如,如下所示在 3x3 的网格中有一个障碍物.[ ...
- Leetcode63.Unique Paths II不同路径2
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为" ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
随机推荐
- C中对整数的大端对齐与小端对齐的理解
示例 /* 大端与小端对齐 说明: 1.对于arm, intel这种x86架构的复杂指令cpu,整数在内存中是 倒着存放的,低地址放低位,高地址放高位,称之为小端对齐 2.对于linux服务器的cpu ...
- ES6——数组
数组: map 映射 ———— 一个对一个 reduce 汇总 ———— 一堆出来一个 算个总数 [1,2,3]==>6 算平均数 [1,2,3]==>2 filter ...
- Vue-cli3 环境的搭建
Vue-cli3 环境的搭建 准备 浏览器插件:Vue.js devtools VsCode 和 VsCode 插件 WebStorm Nodejs vue-cli git 起飞 安装vue-cli3 ...
- vue,一路走来(6)--微信支付
微信支付 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6 分享一下vue实现微信支付.在微信浏览器里面 ...
- Java的GC机制及算法
GC的阶段 对每个对象而言,垃圾回收分为两个阶段:finalization和reclamation. finalization: 指运行这个对象的finalize的方法. reclamation: ...
- java ArrayList存储基本类型
package java06; /* 如果希望像集合ArrayList中存储基本数据类型数据,必须使用基本数据类型对应的“包装类” 基本数据类型 包装类(引用类型,包装类都位于java.lang包下 ...
- token理解
什么是JWT Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点 ...
- RabbitMQ之交换机
1. 交换机类型 rabbitmq常见有四种交换机类型: direct, topic, fanout, headers. 一般headers都不用,工作中用得较多的是fanout,它会将消息推送到所有 ...
- XenServer(服务器虚拟化平台)
Citrix Xenserver,思杰基于Xen的虚拟化服务器.Citrix XenServer是一种全面而易于管理的服务器虚拟化平台,基于强大的 Xen Hypervisor 程序之上.Xen技术被 ...
- vue项目打包之后原本好的样式变得不好了的原因分析
这个主要是打包的过程将所有的css文件进行归类压缩,导致原先其他文件里的样式对当前的产生了影响,应该有同样的类名了.怎么改?要么改类名,要么用scope,scss的写法.