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

  1. [LeetCode题解]24. 两两交换链表中的节点 | 递归

    方法一:递归 解题思路 递归法,假设后续链表已经完成交换,此时只需要对前两个节点进行交换,然后再连接上后续已交换的链表即可. 代码 /** * Definition for singly-linked ...

  2. [LeetCode 题解]: Roman to Interger

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a ro ...

  3. [LeetCode题解]: Sort Colors

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

  4. [LeetCode 题解]: Maximum Subarray

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Find the c ...

  5. [LeetCode 题解]:Gas Station

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 There are ...

  6. [LeetCode 题解]: plusOne

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a no ...

  7. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

  8. LeetCode题解汇总(包括剑指Offer和程序员面试金典,持续更新)

    LeetCode题解汇总(持续更新,并将逐步迁移到本博客列表中) LeetCode题解分类汇总(包括剑指Offer和程序员面试金典) 剑指Offer 序号 题目 难度 03 数组中重复的数字 简单 0 ...

  9. LeetCode题解分类汇总(包括剑指Offer和程序员面试金典,持续更新)

    LeetCode题解汇总(持续更新,并将逐步迁移到本博客列表中) 剑指Offer 数据结构 链表 序号 题目 难度 06 从尾到头打印链表 简单 18 删除链表的节点 简单 22 链表中倒数第k个节点 ...

  10. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

随机推荐

  1. 简洁灵活的前端框架------BootStrap

      前  言 Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加快捷.[1] ...

  2. TCP/IP(四)网络层

    前言 前面给大家介绍了计算机网络的基本概述,物理层和数据链路层.这一篇给大家介绍面试中经常会被问到的网络层.在介绍之前我们回顾一下之前学习的知识! CP/IP协议栈:物理层.链路层.网络层.传输层.应 ...

  3. 安装 node-sass 时报错

    在安装 node-sass 时报错,截图如下 解决方法如下: npm install --save node-sass --registry=https://registry.npm.taobao.o ...

  4. Hadoop 2:Mapper和Reduce

    Hadoop 2:Mapper和Reduce Understanding and Practicing Hadoop Mapper and Reduce 1 Mapper过程 Hadoop将输入数据划 ...

  5. 【2016美团】浏览器和服务器在基于https进行请求链接到数据传输过程中,用到了如下哪些技术

    A.非对称加密技术 B.对称加密技术 C.散列(哈希)算法 D.数字证书 答案:ABCD HTTPS在传输数据之前需要客户端(浏览器)与服务端(网站)之间进行一次握手,在握手过程中将确立双方加密传输数 ...

  6. Mysql连接报错:1130-host ... is not allowed to connect to this MySql server如何处理

    这个问题是因为在数据库服务器中的mysql数据库中的user的表中没有权限(也可以说没有用户),下面将记录我遇到问题的过程及解决的方法. 在搭建完LNMP环境后用Navicate连接出错 遇到这个问题 ...

  7. 自测-5 Shuffling Machine

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  8. Python自学笔记-生成器(来自廖雪峰的官网Python3)

    感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. 生成器 通过列 ...

  9. 使用Dapper操作Mysql数据库

    首先我想说明一下:相比最原始的ADO.NET,一般都认为封装过一层的ORM性能上会有损耗,但其实在使用中你会发现,当你需要把数据库对象转化为实体模型时,很多所谓的DbHelper其实封装的很低效,反而 ...

  10. vue2组件之select2调用

    目前,项目中使用了纯前端的静态项目+RESTFul接口的模式.为了更好的对数据进行操作,前端使用了vue2的mvvm功能,但是由于不是单页面应用,所以,并没有涉及到其它的如vue-route等功能,也 ...