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的更多相关文章

  1. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  2. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  3. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  4. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  5. 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 ...

  6. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  7. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  8. leetcode 280.Wiggle Sort 、324. Wiggle Sort II

    Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...

  9. 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 ...

随机推荐

  1. tcp的三次握手和四次挥手(二)

    一.三次握手 三次握手概念 当面试官问你为什么需要有三次握手.三次握手的作用.讲讲三次握手的时候,我想很多人会这样回答. 首先很多人会先讲下握手的过程: 第一次握手:客户端给服务器发送一个 SYN 报 ...

  2. 如何制作一个Arduino温度数据记录仪

    在本项目中,我们将使用Arduino开发板制作一个温度数据记录仪,该设备从温度传感器LM35获取温度值,并从DS3231实时时钟模块获取时间.然后我们将使用mini SD卡模块将这些值存储在SD卡文件 ...

  3. Clipper库中文文档详解

    简介 Clipper Library(以下简称为Clipper库或ClipperLib或Clipper)提供了对线段和多边形的裁剪(Clipping)以及偏置(offseting)的功能 和其他的裁剪 ...

  4. 让你弄懂 call、apply、bind的应用和区别

    call.apply.bind使用和区别 // 有只猫叫小黑,小黑会吃鱼 const cat = { name: '小黑', eatFish(...args) { console.log('this指 ...

  5. tomcat配置虛擬路徑

    1.server.xml设置 打开Tomcat安装目录,在server.xml中<Host>标签中,增加<Context docBase="硬盘目录" path= ...

  6. Hive窗口函数案例详解

    语法: 分析函数 over(partition by 列名 order by 列名 rows between 开始位置 and 结束位置) 常用分析函数: 聚合类 avg().sum().max(). ...

  7. Base 编解码(转)

    private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0 ...

  8. MongoDB 分片键分类与数据分发

    In sharded clusters, if you do not use the _id field as the shard key, then your application must en ...

  9. P1899 魔法物品

    题目描述 //又是一个好(nan)题好(nan)题 //首先,普通物品一开始就卖掉就可以,因为它不会增值 //至于魔法物品 //如果一个魔法物品使用了卷轴后的价值减去买卷轴的钱还不如鉴定前的价值高,那 ...

  10. Firefox 英文改中文

    小书匠kindle 在Linux上安装好Firefox后,想进设置界面个性化一下,想看看浏览历史,无奈Linux上默认的Firefox是英文设置界面的,对于我种强迫症不能忍. 刚开始还在设置界面下搜索 ...