[leetcode]Unique Paths
来源:互联网 发布:qq群营销软件 编辑:程序博客网 时间:2024/06/06 18:52
题目描述如下:
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 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
解题思路:简单分治思想。定义一个m*n的二维数组matrix,将matrix[i][0]和matrix[0][j]的值设定为1(i的取值为[0,m-1],j的取值范围为[0,n-1]),接下来matrix[i][j]的值就是matrix[i-1][j]与matrix[i][j-1]的和,最后输出matrix[m-1][n-1]即可。
代码如下:
public class Solution { public int uniquePaths(int m, int n) { int [][]matrix = new int[m][n]; int i, j; for(i = 0; i < m; i++) matrix[i][0] = 1; for(j = 0; j < n; j++) matrix[0][j] = 1; for(i = 1; i < m; i++) for(j = 1; j< n; j++) matrix[i][j] = matrix[i - 1][j] + matrix[i][j - 1]; return matrix[m - 1][n - 1]; }}
题目链接:https://leetcode.com/problems/unique-paths/
0 0
- leetcode Unique Paths && Unique Paths
- Leetcode-Unique Paths&Unique PathsII
- [LeetCode] Unique Paths and Unique Paths II
- leetcode--Unique Paths && Unique Paths ii
- leetcode--Unique Paths && Unique Paths ii
- 【Leetcode】【python】Unique Paths/Unique Paths II
- LeetCode: Unique Paths
- LeetCode: Unique Paths II
- [Leetcode] Unique Paths
- [Leetcode] Unique Paths II
- LeetCode : Unique Paths
- LeetCode : Unique Paths II
- leetcode 9: Unique Paths
- [LeetCode] Unique Paths
- [LeetCode] Unique Paths II
- [LeetCode]Unique Paths
- [LeetCode]Unique Paths II
- [LeetCode] Unique Paths
- 人生中第一篇论文,虽然是二作,也记录一下
- Python
- HDU-4983-Goffi and GCD【数论】【欧拉函数】
- Android系统进程间通信(IPC)机制Binder中的Client获得Server远程接口过程源代码分析
- 各种 django 静态文件的配置总结【待续】
- [leetcode]Unique Paths
- 2015年OpenWRT路由器挂载RT3070 USB无线网卡
- Androidx学习笔记(21)-- SQLite 事务
- JavaHello_记事本程序
- Android实战(六)------Android添加按钮事件的几种方法和实例
- 机器学习
- USACO-Section 3.1-PROB Contact
- Androidx学习笔记(22)-- 动态创建TextView展示数据库的数据
- Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析