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. 一个JavaScript触发器插件,可通过指定频次、指定时间内触发指定的处理函数

    js-trigger是一个JavaScript触发器插件,可通过指定频次.指定时间内触发指定的处理函数 Tango<tanwei_yx@126.com> 特性 支持AMD/CMD/Comm ...

  2. JVM菜鸟进阶高手之路九(解惑)

    转载请注明原创出处,谢谢! 在第八系列最后有些疑惑的地方,后来还是在我坚持不懈不断打扰笨神,阿飞,ak大神等,终于解决了该问题.第八系列地址:http://www.jianshu.com/p/7f7c ...

  3. FastDFS安装步骤

    FastDFS是用c语言编写的一款开源的分布式文件系统,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传.下 ...

  4. JAVA多线程---ThreadLocal<E>

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px ".SF NS Text" } tips: 1 当前ThreadLocal ...

  5. asp.net mvc项目实记-开启伪静态-Bundle压缩css,js

    百度这些东西,还是会浪费了一些不必要的时间,记录记录以备后续 一.开启伪静态 如果不在web.config中配置管道开关则伪静态无效 首先在RouteConfig.cs中中注册路由 routes.Ma ...

  6. 第4章 同步控制 Synchronization ----同步机制的摘要

    同步机制摘要Critical Section Critical section(临界区)用来实现"排他性占有".适用范围是单一进程的各线程之间.它是:  一个局部性对象,不是一个核 ...

  7. Hive基础(4)---Hive的内置服务

    版权声明:<—— 本文为作者呕心沥血打造,若要转载,请注明出处@http://blog.csdn.net/gamer_gyt <——   目录(?)[+]   一:Hive的几种内置服务 ...

  8. JSP入门 分页

            <div> <%      Integer pageNo = (Integer) request.getAttribute("pageNo");  ...

  9. 【BZOJ】1015 [JSOI2008]星球大战starwar(并查集+离线处理)

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...

  10. Find The Multiple (poj1426 一个好的做法)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16505   Accepted: 673 ...