leetcode — unique-paths
import java.util.Arrays;
/**
* Source : https://oj.leetcode.com/problems/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).
*
*
* start
* +---------+----+----+----+----+----+
* |----| | | | | | |
* |----| | | | | | |
* +----------------------------------+
* | | | | | | | |
* | | | | | | | |
* +----------------------------------+
* | | | | | | |----|
* | | | | | | |----|
* +----+----+----+----+----+---------+
* finish
*
*
* How many possible unique paths are there?
*
* Above is a 3 x 7 grid. How many possible unique paths are there?
*
* Note: m and n will be at most 100.
*
*/
public class UniquePaths {
/**
* 相当于走迷宫,不过只可以向前或者向下,两个方向
*
* 使用深度优先,递归查找
*
* @param x
* @param y
* @param m
* @param n
* @param result
* @return
*/
public int walk (int x, int y, int m, int n, int result) {
if (x == n - 1 && y == m - 1) {
return result + 1;
}
if (x < n - 1) {
result = walk(x+1, y, m, n, result);
}
if (y < m - 1) {
result = walk(x, y+1, m, n, result);
}
return result;
}
public int finAllUniquePaths (int m, int n) {
if (m <= 0 || n <= 0) {
return 0;
}
return walk(0, 0, m, n, 0);
}
/**
* 使用动态规划
* dp[i][j] = dp[i][j-1] + do[i-1][j]
*
* 边界条件
* 如果i = 0或者 j = 0,dp[0][j] = 1;
*
* @param m
* @param n
*/
public int finAllUniquePathsByDP (int m, int n) {
int[][] maze = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0) {
maze[i][j] = 1;
} else {
maze[i][j] = maze[i-1][j] + maze[i][j-1];
}
}
}
return maze[m-1][n-1];
}
/**
* 动态规划,使用滚动数组
*
* @param m
* @param n
* @return
*/
public int finAllUniquePathsByDPAndScrollArray (int m, int n) {
int[] scrollArray = new int[n];
Arrays.fill(scrollArray, 1);
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
scrollArray[j] += scrollArray[j-1];
}
}
return scrollArray[n -1];
}
public static void main(String[] args) {
UniquePaths uniquePaths = new UniquePaths();
System.out.println(uniquePaths.finAllUniquePaths(1,1));
System.out.println(uniquePaths.finAllUniquePaths(1,2));
System.out.println(uniquePaths.finAllUniquePaths(1,3));
System.out.println(uniquePaths.finAllUniquePaths(2,2));
System.out.println(uniquePaths.finAllUniquePaths(2,3));
System.out.println(uniquePaths.finAllUniquePaths(3,3));
System.out.println("======dp=======");
System.out.println(uniquePaths.finAllUniquePathsByDP(1,1));
System.out.println(uniquePaths.finAllUniquePathsByDP(1,2));
System.out.println(uniquePaths.finAllUniquePathsByDP(1,3));
System.out.println(uniquePaths.finAllUniquePathsByDP(2,2));
System.out.println(uniquePaths.finAllUniquePathsByDP(2,3));
System.out.println(uniquePaths.finAllUniquePathsByDP(3,3));
System.out.println("======dp use scroll array=======");
System.out.println(uniquePaths.finAllUniquePathsByDPAndScrollArray(2,2));
System.out.println(uniquePaths.finAllUniquePathsByDPAndScrollArray(2,3));
System.out.println(uniquePaths.finAllUniquePathsByDPAndScrollArray(3,3));
}
}
leetcode — unique-paths的更多相关文章
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [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 I & II & Minimum Path Sum
Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m ...
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- [leetcode]Unique Paths @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 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 ...
随机推荐
- 字符串、数组、对象常用API
常用的字符串API 1.常见方法和属性 length 属性,获取字符串的字符数量 charAt(i) 返回给定位置的字符 charCodeAt( ) 返回给定位置的字符的字符编码 <scrip ...
- 倒谱(Cepstrum)和线性预测倒谱系数(LPCCs)
倒谱是表示一帧语音数据特征的一个序列.从periodogram estimate of the power spectrum计算得到的倒谱系数,可以用于基音追踪(pitch tracking),然而, ...
- 使用sort对数组中的对象的某个属性进行排序
var data=[ { code: "10004", grade: "5.6", planQuantity: 220, }, { code: "10 ...
- css3用到知识点小结
属性 默认值 属性值及其描述 animation-name 默认值:none 规定需要绑定到选择器的 keyframe 名称. keyframename 规定需要绑定到选择器的 keyframe 的名 ...
- 1030 Travel Plan Dijkstra+dfs
和1018思路如出一辙,先求最短路径,再dfs遍历 #include <iostream> #include <cstdio> #include <vector> ...
- java环境下载
https://repo.huaweicloud.com/java/jdk/8u201-b09/
- 通过iptables添加QoS标记
1.什么是QoS QoS是一种控制机制,它提供了针对不同用户或者不同数据流采用相应不同的优先级,或者是根据应用程序的要求,保证数据流的性能达到一定的水准.QoS的保证对于容量有限的网络来说是十分重要的 ...
- ArcSDE
ArcSDE,即数据通路,是ArcGIS的空间数据引擎,它是在关系数据库管理系统(RDBMS)中存储和管理多用户空间数据库的通路.从空间数据管理的角度看,ArcSDE是一个连续的空间数据模型,借助这一 ...
- TensorFlow学习笔记1
1.daocloud 拉取 docker镜像 docker pull daocloud.io/daocloud/tensorflow:latest 镜像位置: /Users/{YourUserName ...
- Centos7 网络报错Job for iptables.service failed because the control process exited with error code.
今天在进行项目联系的时候,启动在待机的虚拟机,发现虚拟机的网络设置又出现了问题. 我以为像往常一样重启网卡服务就能成功,但是它却报了Job for iptables.service failed be ...