61. Unique Paths && Unique Paths II
Unique Paths
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.
思路: 其实答案就是 C(m+n-2, m-1). 但是写程序利用动态规划会简单快捷。(给两个代码,第一个方便理解,第二个是基于第一个的优化)
1.
class Solution { // C(m+n-2, m-1)
public:
int uniquePaths(int m, int n) {
vector<vector<int> > times(m, vector<int>(n, 0));
for(int r = 0; r < m; ++r) times[r][0] = 1;
for(int c = 1; c < n; ++c) times[0][c] = 1; // 只能到 1 次
for(int r = 1; r < m; ++r)
for(int c = 1; c < n; ++c)
times[r][c] = times[r-1][c] + times[r][c-1];
return times[m-1][n-1];
}
};
2.
class Solution { // C(m+n-2, m-1)
public:
int uniquePaths(int m, int n) {
if(m <= 0 || n <= 0) return 0;
vector<int> R(n, 1); // 一行行的记录
for(int r = 1; r < m; ++r)
for(int c = 1; c < n; ++c)
R[c] = R[c]+ R[c-1];
return R[n-1];
}
};
Unique Paths II
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
.
Note: m and n will be at most 100.
思路:同上,只是最初初始化全 0 . 当前位置为 1 时,则当到达前位置的步数为 0.
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
if(!obstacleGrid.size() || !obstacleGrid[0].size()) return 0;
int m = obstacleGrid.size(), n = obstacleGrid[0].size();
vector<int> R(n, 0);
R[0] = 1-obstacleGrid[0][0];
for(int r = 0; r < m; ++r)
for(int c = 0; c < n; ++c) {
if(c > 0)
R[c] = (obstacleGrid[r][c] == 1 ? 0 : (R[c] + R[c-1]));
else if(obstacleGrid[r][c] == 1) R[0] = 0;
}
return R[n-1];
}
};
61. Unique Paths && Unique Paths II的更多相关文章
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 41. Unique Binary Search Trees && Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- Unique Binary Search Trees,Unique Binary Search Trees II
Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
随机推荐
- 用python定时文章发布wordpress
用python定时文章发布wordpress: 流程: 采集 - 筛选文章 - wordpress文章发布. wordpress文章发布代码:python利用模块xmlrpclib发布文章非常便捷,省 ...
- js中的offsetWidth岁的BUG
---恢复内容开始--- 在js使用offsetWidth来操作控件的运动是会遇到: var oDiv = document.getElementById('div1') oDiv.style.wid ...
- js传url中文参数乱码问题
$("#btnKeyWord").click(function () { window.open("/Atraction/Atraction.aspx?keyword=& ...
- RFID Hacking④:使用ProxMark3 破解门禁
文中提及的部分技术可能带有一定攻击性,仅供安全学习和教学用途,禁止非法使用! 0×00 前言 国际黑客大会Defcon传统之一:开锁!因为黑客认为锁也是一种安全挑战.我们在黑客题材电影.电视剧中也常常 ...
- maven .assembly
配置文件中 配置好Assemblyc插件. 功能:打依赖jar包. java代码如下: <assembly xmlns="http://maven.apache.org/plugins ...
- BZOJ 1833 count 数字计数
sb数位dp. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...
- 期望DP
BZOJ 1415 #include <iostream> #include <cstring> #include <algorithm> #include < ...
- F - To the Max
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...
- iar 数据类型 int folat
8位或者16位的MCU,int占2字节32位的ARM是4字节 则430 int为2字节,则最大值32767,最小值-32768.
- Thrift 个人实战--初次体验Thrift
前言: Thrift作为Facebook开源的RPC框架, 通过IDL中间语言, 并借助代码生成引擎生成各种主流语言的rpc框架服务端/客户端代码. 不过Thrift的实现, 简单使用离实际生产环境还 ...