2014-03-18 01:55

题目:给定一个MxN矩阵,如果某个元素为0,则将对应的整行和整列置为0。

解法:单独挑出一行和一列作为标记数组。因为某元素为0就全部置为0,所以不论A[i][j]为0中的j是几,第i行总会被置为0的。再用O(1)的额外空间去标记单独挑出的那一行一列是否包含0即可。要注意最后清零的顺序和范围不要错了。

代码:

 // 1.7 Write an algorithm such that if an element in an MxN matrx is 0, its antire row and column are set to 0.
#include <cstdio>
#include <vector>
using namespace std; class Solution {
public:
void setMatrixZero(vector<vector<int> > &board) {
int m, n; m = (int)board.size();
if (m == ) {
return;
}
n = (int)board[].size();
if (n == ) {
return;
} int i, j;
bool row0_has_zero = false;
bool col0_has_zero = false;
for (i = ; i < m; ++i) {
if (board[i][] == ) {
col0_has_zero = true;
break;
}
}
for (j = ; j < n; ++j) {
if (board[][j] == ) {
row0_has_zero = true;
}
}
for (i = ; i < m; ++i) {
for (j = ; j < n; ++j) {
if (board[i][j] == ) {
board[i][] = ;
board[][j] = ;
}
}
}
for (i = ; i < m; ++i) {
if (board[i][] == ) {
for (j = ; j < n; ++j) {
board[i][j] = ;
}
}
}
for (j = ; j < n; ++j) {
if (board[][j] == ) {
for (i = ; i < m; ++i) {
board[i][j] = ;
}
}
}
if (row0_has_zero) {
for (j = ; j < n; ++j) {
board[][j] = ;
}
}
if (col0_has_zero) {
for (i = ; i < m; ++i) {
board[i][] = ;
}
}
}
}; int main()
{
vector<vector<int> > board;
int i, j;
int m, n;
Solution sol; while (scanf("%d%d", &m, &n) == && (m || n)) {
board.resize(m);
for (i = ; i < m; ++i) {
board[i].resize(n);
}
for (i = ; i < m; ++i) {
for (j = ; j < n; ++j) {
scanf("%d", &board[i][j]);
}
}
sol.setMatrixZero(board);
for (i = ; i < m; ++i) {
for (j = ; j < n; ++j) {
if (j > ) {
printf(" %d", board[i][j]);
} else {
printf("%d", board[i][j]);
}
}
printf("\n");
}
printf("\n");
} return ;
}

《Cracking the Coding Interview》——第1章:数组和字符串——题目7的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  6. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  7. Cracking the Coding Interview 第一章

    第一章:数组与字符串 1 数组与字符串 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,T ...

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. Cracking the Coding Interview 150题(一)

    1.数组与字符串 1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.假设不允许使用额外的数据结构,又该如何处理? 1.2 用C或C++实现void reverse(char* str)函数, ...

  10. C语言 第七章 数组与字符串

    一.数组 1.1.数组的概念 用来存储一组相同类型数据的数据结构.有点像班上放手机的手机袋,超市的储物柜. 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. ...

随机推荐

  1. nodejs封装的webget webpost方法

    在我之前的项目中,经常用到Nodejs通过post\get方法访问其它网站.webapi.下面是我封装的 Get.Post方法,很适合在一些web字符串收发场景使用(暂不支持文件.二进制流等传输). ...

  2. 323. Number of Connected Components in an Undirected Graph (leetcode)

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  3. php之判断点在多边形内的api

    1.判断点在多边形内的数学思想:以那个点为顶点,作任意单向射线,如果它与多边形交点个数为奇数个,那么那个点在多边形内,相关公式: <?php class AreaApi{ //$area是一个多 ...

  4. which,whereis,locate,find

    which      查看可执行文件的位置 [root@redhat ~]# which passwd /usr/bin/passwd which是通过 PATH 环境变量到该路径内查找可执行文件,所 ...

  5. Miller rabin

    蛤蛤,终于基本上搞懂了 #include<iostream> #include<cstdio> using namespace std; long long num[10]={ ...

  6. 递归遍历目录拷贝cdh下的lib到一个目录

    destpath='/home/hadoop/soft/hadoop-2.0.0-cdh4.5.0/cdhlib/'jarpath='/home/hadoop/soft/hadoop-2.0.0-cd ...

  7. Spring整合JUnit spring静态对象属性的注入

    package cn.itcast.d_junit4; import org.junit.Test; import org.junit.runner.RunWith; import org.sprin ...

  8. java基础 java中枚举的应用 抽象方法问题

    package com.swift.meiju; import org.junit.Test; public class Demo{ @Test public void test() { System ...

  9. UVa中国麻将(Chinese Mahjong,Uva 11210)

    简单的回溯题 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm ...

  10. Linux 个人服务搭建脱坑实录

    环境:VMware.Centos7 64位.jdk1.7.Tomcat 7 说明:本是个人的爬坑经历所总结出来的,记录一下心得.也给有需要的人提供一些帮助.引用了一些大神的笔记,非常感谢,希望大神们不 ...