《Cracking the Coding Interview》——第1章:数组和字符串——题目7
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的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- 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 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview 第一章
第一章:数组与字符串 1 数组与字符串 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,T ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Cracking the Coding Interview 150题(一)
1.数组与字符串 1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.假设不允许使用额外的数据结构,又该如何处理? 1.2 用C或C++实现void reverse(char* str)函数, ...
- C语言 第七章 数组与字符串
一.数组 1.1.数组的概念 用来存储一组相同类型数据的数据结构.有点像班上放手机的手机袋,超市的储物柜. 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. ...
随机推荐
- UOJ #207. 共价大爷游长沙
#207. 共价大爷游长沙 链接:http://uoj.ac/problem/207 题意:给一棵树,要求支持加边.删边.询问一条边是否被所有路径覆盖.同时路径端点集合有加入与删除操作. 想法: 考虑 ...
- 会说话的ABAP report
report z. INCLUDE ole2incl. DATA: ole TYPE ole2_object, voice TYPE ole2_object, text ...
- less通用pc移动库
// less 文件 (移动端通用less文件) // 作者 marchen // 时间 2014/9/1 // 协议 MIT // 只考虑webkit内核手机浏览器和火狐内核浏览器 // 自定义le ...
- oracle 创建SDO_Geometry表
Oracle Spatial由一坨的对象数据类型,类型方法,操作子,函数与过程组合而成.一个地理对象作为一个SDO_GEOMETRY对象保存在表的一个字段里.空间索引则由普通的DDL和DML语句来建立 ...
- codeforces 600E Lomsat gelral
题面:codeforces600E 学习一下$dsu \ on \ tree$.. 这个东西可以处理很多无修改子树问题,复杂度通常为$O(nlogn)$. 主要操作是:我们先把整棵树链剖一下,然后每次 ...
- 火车进站输出路径(HDU1022)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 解题报告: 思路: 就是维护好这个栈,只要它不是空,并且头部和ans相同,就一直出栈,直到不满足 ...
- 深入理解MyBatis-Spring中间件(spring/mybatis整合)
转:http://blog.csdn.net/fqz_hacker/article/details/53485833 Mybatis-Spring 1.应用 mybatis是比较常用的数据库中间件,我 ...
- selenium+chrome下载文件,格式怎么选择???
学习了下载 if browser == "Chrome": options=webdriver.ChromeOptions() prefs={'profile.default_co ...
- SecureCRT 设置
- Python爬虫,看看我最近博客都写了啥,带你制作高逼格的数据聚合云图
转载请标明出处: http://blog.csdn.net/forezp/article/details/70198541 本文出自方志朋的博客 今天一时兴起,想用python爬爬自己的博客,通过数据 ...