leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths
class Solution {
public:
int uniquePaths(int m, int n) {
if(m <= || n <= )
return ;
vector<vector<int> > dp(m,vector<int>(n));
dp[][] = ;
for(int i = ;i < m;i++)
dp[i][] = ;
for(int i = ;i < n;i++)
dp[][i] = ;
for(int i = ;i < m;i++){
for(int j = ;j < n;j++){
dp[i][j] = dp[i-][j] + dp[i][j-];
}
}
return dp[m - ][n - ];
}
};
63. Unique Paths II
leetcode的例子中int会越界,所以需要用long
与Unique Paths I不同在于多了障碍物,障碍物的情况直接为0就好,在初始化的时候需要做这个操作,在dp的迭代过程中也要做,其他与Unique Paths I 是一样的
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
if(m <= )
return ;
int n = obstacleGrid[].size();
if(n <= )
return ;
if(obstacleGrid[][] == || obstacleGrid[m-][n-] == )
return ;
vector<vector<long> > dp(m,vector<long>(n));
dp[][] = ;
for(int i = ;i < m;i++){
if(dp[i-][] == || obstacleGrid[i][] == )
dp[i][] = ;
else
dp[i][] = ;
}
for(int i = ;i < n;i++){
if(dp[][i-] == || obstacleGrid[][i] == )
dp[][i] = ;
else
dp[][i] = ;
}
for(int i = ;i < m;i++){
for(int j = ;j < n;j++){
if(obstacleGrid[i][j] == )
dp[i][j] = ;
else
dp[i][j] = dp[i-][j] + dp[i][j-];
}
}
return dp[m-][n-];
}
};
leetcode 62. Unique Paths 、63. Unique Paths II的更多相关文章
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- leetcode 280.Wiggle Sort 、324. Wiggle Sort II
Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
随机推荐
- Mysql安装与问题合集
下载mysql https://dev.mysql.com/downloads/mysql/ 下载历史版本 看这篇文章 https://www.cnblogs.com/reyinever/p/8551 ...
- 0014SpringBoot结合thymeleaf实现登录功能
该登录功能需要实现的需求如下: 1.输入用户名密码,如果验证通过,进入首页,并显示登录的用户名 2.如果验证不通过,则重新进入登录页面,并显示“用户名密码错误” 3.如果未经登录,不能直接访问首页等静 ...
- ABCD组·第五次团队作业项目需求分析改进与系统设计
项目 内容 这个作业属于哪个课程 http://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh 团队 ...
- Nginx入门(四)——反向代理
server { listen 8020; server_name localhost; location / { root html; index index.html index.htm; pro ...
- git 将master分支合到自己的开发分支
背景: 一般开发自己的分支都是从最新的master上拉取,但中间master会有改动,此时需要将最新的master合到自己的分支中 命令: 1. 查看当前的分支,星号标识为当前分支:(如果查询结果有m ...
- Selenium常用API的使用java语言之17-文件上传
对于通过input标签实现的上传功能,可以将其看作是一个输入框,即通过sendKeys()指定本地文件路径的方式实现文件上传. 创建upfile.html文件,代码如下: <html> & ...
- [TJOI2015]弦论(第k小子串)
题意: 对于一个给定的长度为n的字符串,求出它的第k小子串. 有参数t,t为0则表示不同位置的相同子串算作一个,t为1则表示不同位置的相同子串算作多个. 题解: 首先,因为t的原因,后缀数组较难实现, ...
- leetcode解题报告(21):Majority Element
描述 Given an array of size n, find the majority element. The majority element is the element that app ...
- [Luogu] 送花
https://www.luogu.org/problemnew/show/2073 自己yy,明显错 #include <bits/stdc++.h> using namespace s ...
- Codeforces Round #591
目录 Contest Info Solutions A. Save the Nature B. Sequence Sorting C. Paint the Tree D. Stack Extermin ...