Leetcode题解(24)
73. Set Matrix Zeroes

分析:如果没有空间限制,这道题就很简单,但是要求空间复杂度为O(1),因此需要一些技巧。代码如下(copy网上的代码)
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix)
{
bool bColZero = false, bRowZero = false;
if (matrix.size() == || matrix[].size() == )
{
return;
}
// Mark bColZero true when col[0] contains zero.
for (size_t row = ; row < matrix.size(); ++row)
{
if (!matrix[row][]) bColZero = true;
}
// Mark bRowZero true when row[0] contains zero.
for (size_t col = ; col < matrix[].size(); ++col)
{
if (!matrix[][col]) bRowZero = true;
}
// Map zero points to row[0] and col[0].
for (size_t row = ; row < matrix.size(); ++row)
{
for (size_t col = ; col < matrix[row].size(); ++col)
{
if (!matrix[row][col])
{
matrix[][col] = ;
matrix[row][] = ;
}
}
}
// Set zero according to row[0] and col[0].
for (size_t row = ; row < matrix.size(); ++row)
{
for (size_t col = ; col < matrix[row].size(); ++col)
{
if (!matrix[row][] || !matrix[][col])
{
matrix[row][col] = ;
}
}
}
// Process col[0].
if (bColZero)
{
for (size_t row = ; row < matrix.size(); ++row)
{
matrix[row][] = ;
}
}
// Process row[0].
if (bRowZero)
{
for (size_t col = ; col < matrix[].size(); ++col)
{
matrix[][col] = ;
}
}
}
};
------------------------------------------------------------------------------分割线-------------------------------------------------------------------
74. Search a 2D Matrix
题目

分析,这道题目在《剑指offer》上出现过,思想是分段查找,只是查找的起点是右上角的元素,代码如下:
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int i = , j = matrix[].size() - ;
while (i < matrix.size() && j >= )
{
if (target == matrix[i][j])
return true;
else if (target < matrix[i][j])
j--;
else
i++;
}
return false;
}
};
--------------------------------------------------------------------分割线------------------------------------------------------------------------------
75. Sort Colors
题目

分析:简单题目,可以直接统计0,1,2的个数,然后赋值即可
代码如下:
class Solution {
public:
void sortColors(vector<int>& nums) {
int size = nums.size();
int zero=,one=,two = ;
int i,j;
for(i=;i<size;i++)
{
if( == nums[i])
zero++;
else if( == nums[i])
one++;
else
two++;
}
i=;
j=;
for(j=;j<zero;j++)
nums[i++]=;
for(j=;j<one;j++)
nums[i++]=;
for(j=;j<two;j++)
nums[i++]=;
}
};
Leetcode题解(24)的更多相关文章
- [LeetCode题解]24. 两两交换链表中的节点 | 递归
方法一:递归 解题思路 递归法,假设后续链表已经完成交换,此时只需要对前两个节点进行交换,然后再连接上后续已交换的链表即可. 代码 /** * Definition for singly-linked ...
- [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题解汇总(包括剑指Offer和程序员面试金典,持续更新)
LeetCode题解汇总(持续更新,并将逐步迁移到本博客列表中) LeetCode题解分类汇总(包括剑指Offer和程序员面试金典) 剑指Offer 序号 题目 难度 03 数组中重复的数字 简单 0 ...
- LeetCode题解分类汇总(包括剑指Offer和程序员面试金典,持续更新)
LeetCode题解汇总(持续更新,并将逐步迁移到本博客列表中) 剑指Offer 数据结构 链表 序号 题目 难度 06 从尾到头打印链表 简单 18 删除链表的节点 简单 22 链表中倒数第k个节点 ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
随机推荐
- 我的python学习笔记一
我的python学习笔记,快速了解python,适合有C语言基础的. http://note.youdao.com/noteshare?id=93b9750a8950c6303467cf33cb1ba ...
- 封装好的图片滑动框架(AndroidImageSlider)
前言 广告轮播条的重要性不言而喻.在很多类型app中出场率都很高. 今天给大家介绍一个轮播图开源项目,这个项目把轮播图需要的ViewPager跟计时器做了封装,使用极其方便,支持gradle在线依赖. ...
- Integer的自动拆箱
public class Test2{ public static void main(String[] args){ Integer a=1; Integer b=2; Integer c=3; I ...
- APUE 4 - 线程
对传统的UNIX进程来讲,一个进程中只有一个线程,这就意味着一个进程在同一时刻只能做一件事(即使是多核CPU).使用多线程技术, 我们可以设计程序使得一个进程在同一时刻做多件事.使用多线程编程具有以下 ...
- 在Ubuntu上安装arm-linux-gcc的问题
由于之前将Ubuntu的更新关掉了,所以导致我下载32位兼容包一直出错. 在arm-linux-gcc 安装之后,还不能编译程序的话,首先看自己的系统是多少位的,因为网上大部分的安装包都是32位的,所 ...
- Judge Route Circle
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...
- Linux查找和筛选工具
本文为原创文章,转载请标明出处 目录 文件名通配符 单字符匹配元字符 ? 多字符匹配元字符 * 字符范围匹配符 [] 排除范围匹配符 [!] 命令中的正则表达式 单字符匹配符 . 单字符或字符串重复匹 ...
- Python实战之文件操作的详细简单练习
['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__' ...
- less使用ch1--认识语法
@charset "utf-8"; //注释------------------------------ /*我是可以被编译出来的*/ //不能被编译出来 //变量-------- ...
- HashMap实现原理
学习笔记之HashMap篇,简单学习了解HashMap的实现原理和扩容. 大家都知道HashMap处理数据很快,时间复杂度O(1),那么是怎么做到的呢?那就先了解一下常见数据结构. 一般来说,我们把存 ...