动态规划练习1--Unique Paths
2019独角兽企业重金招聘Python工程师标准>>>
题目内容
[引自] https://leetcode.com/problems/unique-paths/description/
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.
伪代码
核心: uniquePaths(i,j)=uniquePaths(i-1,j)+uniquePaths(i,j-1)
states数组保存状态值 states[i][j]表示到达i,j位置的不同路径数
边界值:初始边界为(0,x)和 (x,0)
初始化: states[0][0]=0,states[0][x]=0,states[x][0]=0
(1,1)表示初始位置,states[1][1]=0,states[1][2]=1,states[2][1]=1
算法
int uniquePaths(int m, int n)int[][] states =new int[101][101]states[1][1]=1for i=1 to m+1for j=1 to n+1if(i==1 and j==1)continue;states[i][j]=states[i-1][j]+states[i][j-1]states[1][1]=0return states[m][n]
java 代码实现
public int uniquePaths(int m, int n) {int[][] states =new int[101][101];states[1][1]=1;for(int i=1;i<m+1;i++){for(int j=1;j<n+1;j++){if(i==1&&j==1){continue;}states[i][j]=states[i-1][j]+states[i][j-1];}}states[1][1]=0;return states[m][n];}
转载于:https://my.oschina.net/u/3706181/blog/1605610
动态规划练习1--Unique Paths相关推荐
- leetcode 62, 63, 980. Unique Paths I, II, III | 62, 63, 980. 不同路径 I, II, III(暴力递归->傻缓存->动态规划)
62. Unique Paths https://leetcode.com/problems/unique-paths/ 注意本题只能向右 / 向上走. DP 问题,经典又熟悉. 暴力递归->傻 ...
- 【动态规划】LeetCode 62. Unique Paths
LeetCode 62. Unique Paths Solution1:我的未能AC的答案 递归超时了!!! class Solution { public:int uniquePaths(int m ...
- 10.2 动态规划算法套路及空间优化 —— Climbing Stairs Unique Paths
这一篇文章从最简单的动态规划题目开始,结合上一节动态规划三要素,以LeetCode两道基础的DP题目阐述DP问题的基本套路解法. 70. Climbing Stairs You are climbin ...
- 【动态规划】LeetCode 63. Unique Paths II
LeetCode 63. Unique Paths II Solution1:我的答案 在哪里做过这题? class Solution { public:int uniquePathsWithObst ...
- 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). ...
- [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). ...
- leetcode 【 Unique Paths 】python 实现
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- 63. Unique Paths II and 64. Minimum Path Sum
文章目录 1 63 Unique Paths II 1.1 题目描述 1.2 动态规划解决 2 64. Minimum Path Sum 2.1 题目理解 2.2 动态规划 这一遍刷dp的题目就很轻松 ...
- LeetCode62 Unique Paths
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- LeetCode 1.Minimum Path Sum 2.Unique Paths I and II
大家好,我是刘天昊,快到端午节了,今天说两道动态规划的题目(话说动规真的挺难的) 当然这三题是一样的解体思路先看Unique Paths A robot is located at the top-l ...
最新文章
- windows 10 代理服务器出现问题
- Nginx学习之五:Nginx第三方模块
- Nginx(六)-- 配置文件之Gzip
- [react] 举例说明useState
- 学习笔记3-C语言-基础
- springboot2——MyBatis入门
- 划重点!DWS开发的五大要点
- yii2 关掉php notice,php – 使用Yii2中的自动填充更新隐藏字段
- TC SRM 665 DIV2 B LuckyCycle 暴力
- 大师之路解惑基础概念篇:Tomcat与servlet联系与区别
- linux 关机自动重启,自己动手解决了Ubuntu关机后自动重启
- matlab实现色彩迁移,图像的色彩风格迁移
- 系统试运行报告是谁写的_深圳个人信用报告查询系统入口https://ipcrs.pbccrc.org.cn/...
- C#【EF Core框架】使用乐观锁处理并发冲突
- 飞腾cpu服务器浪潮信息,浪潮发布基于国产飞腾CPU的服务器
- 简单的图片轮播器(一):一个关于仿flash的图片轮换器
- Flex布局——flex-basis、flex-grow、flex-shrink
- 创意信息子公司作为华为合作伙伴 携产品登录华为云端展厅
- ARM NandFlash 介绍
- python 写出好看且实用的后台管理-django 后台管理美化