Leetcode题解(21)
62. Unique Paths
题目
分析:
机器人一共要走m+n-2步,现在举个例子类比,有一个m+n-2位的二进制数,现在要在其中的m位填0,其余各位填1,一共有C(m+n-2,m-1)种可能,如果0表示向下走,1表示向右走,这样就和题目意思一样了。
现在考虑最后一步的走法,要么向右走到达终点,要么向下走到达终点,因此
f(m,n) = f(m,n-1)+f(m-1,n);
代码如下(主要考虑的是大数据):
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> v(m, vector<int>(n, ));
for(int i=; i<m; ++i){
for(int j=; j<n; ++j){
v[i][j]=v[i-][j]+v[i][j-];
}
}
return v[m-][n-];
}
};
----------------------------------------------------------------------------分割线----------------------------------------------------------------------
63、Unique Paths II
题目
分析:这一题和62题的思路是一样,都是采用递推公式f(m,n) = f(m,n-1)+f(m-1,n);只不过在障碍处,f(m,n)=0
代码如下:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[].size();
vector<vector<int>> v(m, vector<int>(n, ));
for(int i=;i<n;)
if(obstacleGrid[][i] == )
{ while(i<n)
{
v[][i] = ;
i++;
}
}
else
{
v[][i] = ;
i++;
}
for(int i=;i<m;)
if(obstacleGrid[i][] == )
{ while(i<m)
{
v[i][] = ;
i++;
}
}
else
{
v[i][] = ;
i++;
}
for(int i=; i<m; ++i){
for(int j=; j<n; ++j){
if(obstacleGrid[i][j] == )
v[i][j] = ;
else
v[i][j]=v[i-][j]+v[i][j-];
}
}
return v[m-][n-];
}
};
---------------------------------------------------------------------------------分割线-----------------------------------------------------------------
64. Minimum Path Sum
题目
分析:f(m,n) = min(f(m,n-1),f(m-1,n))+a[m][n]
代码如下
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[].size();
vector<vector<int>> v(m, vector<int>(n, ));
int temp=; for(int i = ;i<n;i++)
{
v[][i] = temp + grid[][i];
temp = v[][i];
}
temp=;
for(int i = ;i<m;i++)
{
v[i][] = temp + grid[i][];
temp = v[i][];
}
//v[0][0] = v[0][0]-grid[0][0]; for(int i=; i<m; ++i){
for(int j=; j<n; ++j){
if(v[i-][j]>v[i][j-])
temp = v[i][j-];
else
temp = v[i-][j]; v[i][j] = grid[i][j]+temp;
}
}
return v[m-][n-];
} };
Leetcode题解(21)的更多相关文章
- [LeetCode题解]21. 合并两个有序链表 | 递归
解题思路 使用递归实现: 定义函数功能:合并两个有序链表,并返回链表的头 结束条件:两个链表其中一个为空,返回另一个链表 递推公式: l1.val < l2.val:l1.next = Merg ...
- 【LeetCode题解】7_反转整数
目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...
- [LeetCode 题解]: Roman to Interger
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a ro ...
- [LeetCode题解]: Sort Colors
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an a ...
- [LeetCode 题解]: Maximum Subarray
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Find the c ...
- [LeetCode 题解]:Gas Station
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 There are ...
- [LeetCode 题解]: plusOne
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a no ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- LeetCode题解: LRU Cache 缓存设计
LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode 版权声明:本文为博主原创文章,遵循CC 4 ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
随机推荐
- 静态页面如何实现 include 引入公用代码
一直以来,我司的前端都是用 php 的 include 函数来实现引入 header .footer 这些公用代码的,就像下面这样: <!-- index.php --> <!DOC ...
- mongoDB学习手记2--建库、删库、插入、更新
上一篇 讲了在windows系统下的安装和启动,本文主要讲怎么建库.删库.插入.更新 在讲之前我们说一下mongoDB的一些基本概念,我们对比关系型数据库能更直观的理解 SQL术语/概念 Mongo ...
- Hive 存储类型 StoreType
file_format: : SEQUENCEFILE | TEXTFILE -- (Default, depending on hive.default.fileformat configurati ...
- H5音频处理的一些小知识
前 言 LiuDaP 十一过后,小编要做一个关于音乐播放器的项目,要用到大量H5音频处理的内容,于是在十月一日国庆黄金周闲暇之际,自己学习了一下H5音频的相关内容.虽然自学的没有那么深入,但是对 ...
- UWP brush
---some words---- 1.Alpha:透明度 2.Argb :Alpha red green blue 3.brush: [brʌʃ] 刷子,笔画,笔刷 4.fore 前头 5.F ...
- input 事件与汉字输入法:使用compositionend事件解决
input 事件与汉字输入法:使用compositionend事件解决 在使用<input type="text">的input事件的时候 会遇到中文输入法的" ...
- 关于ios11 tableView和ScrollView受导航栏影响向下偏移的问题
看到网上说法ios11中automaticallyAdjustsScrollViewInsets属性被废弃,所以要设置tableView.contentInsetAdjustmentBehavior ...
- Python实战之Selenium自动化测试web登录
#!/usr/bin/env python3 # -*- coding:utf-8 -*- from selenium import webdriver from selenium.webdriver ...
- less使用ch1--简单使用
1 ku.less .reset(){ *{margin:0;padding:0;} ul.ol{list-style: none;} a{text-decoration: none;} img{bo ...
- ch7-列表渲染(v-for key 数组更新检测 显示过滤/排序结果)
1 说明 我们用 v-for 指令根据一组数组的选项列表进行渲染. v-for 指令需要以 item in items 形式的特殊语法, items 是源数据数组并且 item 是数组元素迭代的别名. ...