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

  1. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  2. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

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

  4. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  5. Unique Binary Search Trees,Unique Binary Search Trees II

    Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given  ...

  6. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

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

  8. 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]* ...

  9. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

随机推荐

  1. python 从文件导入分类

    # -*- coding:utf-8 -*- """ 从文件导入分类 根据行首制表符或空格确定层级关系(4个空格等于一个制表符 同一行制表符和空格不能混用 ) 必须是 u ...

  2. bicycle

    http://bj.ganji.com/zixingchemaimai/611076069x.htm

  3. JDBC中的PreparedStatement

    PreparedStatement类从Statement中继承来. 可以将SQL语句传给数据库做编译处理,即在执行的SQL语句中包含一个或多个IN参数,可以设置IN参数值多次执行SQL语句,不必重新给 ...

  4. HDU2222

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 注意: 1. keyword可以相同,因此计算时要累计:cur->num++. 2. 同一个keyw ...

  5. iOS 根据UIImage 修改UIImageView Frame (包括截取图片中间部分)

    iOS UIImageView 根据需求调整frame 1.图片的宽和高不相等,截取图片的中间部分,截取的部分Size明确 2.图片的宽度要等于其父视图的类的宽度,然后根据宽度计算高度,保证 图片不变 ...

  6. Android TextView图文混合编排

    Android TextView图文混合编排 实现技术细节不难,两个要点:1.html代码的混合编写.2,重写ImageGetter.例如:布局: <?xml version="1.0 ...

  7. UVALive 7302 (最短路)

    Probelm Terrorists 题目大意 给一张n个点,m条边的无向图.共有q个询问,每次询问u到v的最短路. n <= 100000 ,  n-1 <= m <= n + 5 ...

  8. Using python to process Big Data

    Pandas is a great lib to process BIg Data. 1) pandas.pivot_table(data,values=None,columns=None,aggfu ...

  9. C#中using关键字的作用及用法

    using的用途和使用技巧. 1.  引用命名空间 2.  为命名空间或类型创建别名 3.  使用using语句 1.  引用命名空间,这样可以在程序中引用命名空间的类型而不必指定详细的命名空间. a ...

  10. H5版定点投篮游戏(1)--物理模型抽象

    前言: 前几天目睹了大学同学开了个微店, 算是间接体验微信公众平台的使用. 觉得非常便捷和方便, 于是自己也想捣鼓一个. 公众号取名: "木目的H5游戏世界", 定位做成一个, 个 ...