1. Follow up for "Unique Paths":
  2.  
  3. Now consider if some obstacles are added to the grids. How many unique paths would there be?
  4.  
  5. An obstacle and empty space is marked as 1 and 0 respectively in the grid.
  6.  
  7. For example,
  8.  
  9. There is one obstacle in the middle of a 3x3 grid as illustrated below.
  10.  
  11. [
  12. [0,0,0],
  13. [0,1,0],
  14. [0,0,0]
  15. ]
  16.  
  17. he total number of unique paths is 2.
  18.  
  19. Note: m and n will be at most 100.

  同Unique Paths的DP类似。

  1. class Solution {
  2. public:
  3. int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
  4. // Start typing your C/C++ solution below
  5. // DO NOT write int main() function
  6. int m = obstacleGrid.size();
  7. int n = obstacleGrid[].size();
  8. vector<vector<int>> grid(m, vector<int>(n,));
  9. for(int i = ; i< n; i++)
  10. if(obstacleGrid[][i] ==) grid[][i] = ;
  11. else break;
  12. for(int i = ; i< m; i++)
  13. if(obstacleGrid[i][] == ) grid[i][] = ;
  14. else break;
  15.  
  16. for(int i = ; i< m; i++)
  17. for(int j = ; j< n; j++)
  18. if(obstacleGrid[i][j] == )
  19. grid[i][j] = grid[i-][j] + grid[i][j-];
  20.  
  21. return grid[m-][n-];
  22. }
  23. };

LeetCode_Unique Paths II的更多相关文章

  1. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  2. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  3. 【leetcode】Unique Paths II

    Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...

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

  5. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  6. 【LeetCode练习题】Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  7. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

  8. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  9. LEETCODE —— Unique Paths II [Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

随机推荐

  1. BZOJ3538: [Usaco2014 Open]Dueling GPS

    3538: [Usaco2014 Open]Dueling GPS Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 59  Solved: 36[Subm ...

  2. 51nod-正整数分组问题(基础方程DP-01背包)

    正整数分组 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. 思路: 这题的实质其实也是0-1背包问 ...

  3. windows server2012域服务器降级的方法

    最近在一个新的网络环境里建立windows域服务器,准备建立主.备两台域服务器.在一台新服务器上面安装了windows2012R2并配置了DC服务.域和林的级别设置为Windows2012R2级别.在 ...

  4. [深入React] 6.组件

    组件是react的大杀器,超出其他框架几百里 react 组件和dom一样也是树状结构,只能由上而下传递变量(或者调用),不可以兄弟间或者更远的发生关系,为的就是简单,而且工作的很好. 每个组件有自己 ...

  5. 转义字符和ASCII

    一.字符(char)   数字(int)   屏幕显示 '\n'                      10                   换行 '\0'                   ...

  6. java 截取字符串 拆分字符串

    例如 想要吧"90_python" 分成“90” 和“python” 从网上看到的方法: public class splitTest { public static void m ...

  7. Android开发之发送短信

    本实例通过SmsManager的sendTextMessage方法实现发送短信关于SmsManager的具体解释大家能够參照:Android开发之SmsManager具体解释 实例执行效果图: 程序代 ...

  8. Android SDK Manager 无法更新SDK

    Android SDK Manager 被墙后无法更新SDK 下载sdk时抛出错误:Failed to fetch URL http://dl-ssl.google.com/ 參考例如以下博客: ht ...

  9. 完全卸载sql2005

    转自:http://www.cnblogs.com/mytechblog/articles/1883961.html 1.Stop 所有服务 2.在控制面板中卸载所有与SQL Server 2005相 ...

  10. speex的基本编码和解码流程

    最近在研究speex的编码和解码流程 之前在IM上用到的都是发语音片段,这个很简单,只需要找到googlecode上gauss的代码,然后套一下就可以用了. 不过googlecode要关闭,有人将他导 ...