【leetcode】 Unique Path ||(easy)
Follow up for "Unique Paths":
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1
and 0
respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2
.
思路:跟Unique Paths差不多,就是把有障碍的地方方法数变成0,注意左上角为障碍的情况
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
if(obstacleGrid.empty())
{
return ;
}
vector<vector<int>> ways(obstacleGrid.size(), vector<int>(obstacleGrid[].size(),));
if(obstacleGrid[][] == )
{
ways[][] = ;
}
for(int c = ; c < obstacleGrid[].size(); c++)
{
ways[][c] = (obstacleGrid[][c] == ) ? : ways[][c - ];
}
for(int r = ; r < obstacleGrid.size(); r++)
{
ways[r][] = (obstacleGrid[r][] == ) ? : ways[r - ][];
}
for(int i = ; i < obstacleGrid.size(); i++)
{
for(int j = ; j < obstacleGrid[].size(); j++)
{
ways[i][j] = (obstacleGrid[i][j] == ) ? : ways[i - ][j] + ways[i][j - ];
}
}
return ways[obstacleGrid.size() - ][obstacleGrid[].size() - ];
}
};
【leetcode】 Unique Path ||(easy)的更多相关文章
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【leetcode】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- 【leetcode】 Unique Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【leetcode】Minimum Path Sum(easy)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【题解】【矩阵】【DP】【Leetcode】Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...
- 【LeetCode】437. Path Sum III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...
- 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- 【leetcode】Unique Binary Search Trees (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- eclipse tomcat debug启动慢
myeclipse或eclipse下debug模式启动很慢,默认模式也是debug,网上找了终于解决, 原因是有eclipse或myeclipse启动debug时自动添加断点,所以必须删除一些东西. ...
- Swift2.1 语法指南——错误处理
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- 第九天 iOS音频技术
1. AQRecorder mRecordFormat.mFormatID = inFormatID; if (inFormatID == kAudioFormatLinearPCM) { // if ...
- 用jQuery编的一个分页小代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android学习笔记(二十)——自定义内容提供器
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 如果我们想要实现跨程序共享数据的功能,官方推荐的方式就是使用内容提供器,可以通过新建一个类去继承 Conten ...
- Sqli-LABS通关笔录-15
这关是延时的了. Payload: -admin' or sleep(10)# 需要注意的是,--+不行反而#才可以.具体缘由可见<sql注入之你问我答>第20问:http://www.c ...
- Failed to resolve: junit:junit:4.12
在Android Studio创建项目之后,提示一个junit错误. 解决方案: 第一步:找到build.gradle的file,如图: 第二步: 第三步:把中间行代码"testCompi ...
- eclipse添加velocity项目
1.首先添加jar包,记得包含以下的主要两个类别 2.新建一个servlet类(继承自VelocityViewServlet) package com.servlet; import java.uti ...
- (转)android中颜色矩阵colormatrix
原文地址:http://www.cnblogs.com/menlsh/archive/2013/02/03/2890888.html 在Android中,对图像进行颜色方面的处理,如黑白老照片.泛黄旧 ...
- 2-python学习——hello world
"hello world"是编程界一个经久不衰的例子,几乎所有语言的学习教程都把它当做第一个程序的范例.学习的过程就是再造轮子的过程,千万不要以为有人做过的,就不去学习了. hel ...