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)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
随机推荐
- 【】小技巧】CSS文字两端对齐
需求如下,红框所在的文字有四个字的.三个字的.两个字的,如果不两端对齐可以选择居中对齐,或者右对齐.但是如果要像下面这样两端对齐呢? 我相信以前很多人都这么干过:两个字中间使用 来隔开达到四个字的宽度 ...
- 关于使用git和github的一点点感想
第二篇博客 首先附上我的第一个java程序github地址: https://github.com/KingsC123456/FirstJavaHello 其次是关于我的github介绍,因为一直使用 ...
- 【个人笔记】《知了堂》MySQL中的数据类型
MySQL中的数据类型 1.整型 MySQL数据类型 含义(有符号) tinyint(m) 1个字节 范围(-128~127) smallint(m) 2个字节 范围(-32768~32767) ...
- 王者荣耀_KEY
WZRY 为了排位赛的Cjj神,最近耗尽气力来打WZRY. Cjj神最近有N局预约的排位赛,其中第i局需要耗时Li的时间.因为浓浓的Gay情,Cjj神不能改变这些排位赛的的顺序.作为一个很有(mei) ...
- Codevs1380没有上司的舞会_KEY
没有上司的舞会 1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系 ...
- Eclipse中添加文档注释快捷键
该博客仅记录自己添加文档注释时的操作,由于参考文档地址忘了,因此如果与其他文档重复,请见谅 以下是我的操作过程: 例如: /** * @param * @return */ 快捷键为: ...
- ThinkPHP中:使用递归写node_merge()函数
需求描述: 现有一个节点集合 可以视为一个二维数组 array(5) { [0] => array(4) { ["id"] => string(1) "1&q ...
- 针对Openlayer3官网例子的简介
网址:http://openlayers.org/en/latest/examples/ 如果大家想了解ol3能做什么,或者说已提供的API有什么,又闲一个个翻例子跟API累的话,就看看这个吧. 1. ...
- Hive任务优化(1)
一个Hive查询生成多个Map Reduce Job,一个Map Reduce Job又有Map,Reduce,Spill,Shuffle,Sort等多个阶段,所以针对Hive查询的优化可以大致分为针 ...
- Longest Uncommon Subsequence I
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...