《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型,数组里的数据成为元素. ...
随机推荐
- 在IE中解决当前安全设置不允许下载该文件的方案
解决方案一: 1.0打开IE后,单击菜单栏中的“工具”菜单,在弹出的菜单中选择“Internet选项”命令: 2.0在弹出“Internet选项”的对话框中,打开“Internet选项”对话框: 3. ...
- MVC中某个页面不需要引用母版页的正确写法
有些页面想使用单独的样式不想用母版页的时候,可以在开始声明下,就可以不用母版页的CSS和JS引用了语法如下: @{Layout = "";} . 非常之简单
- 剑指offer 33 把数组排成最小的数
错误代码 class Solution { public: int FindGreatestSumOfSubArray(vector<int> array) { int length = ...
- Django Reverse for 'artic_post' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] ...
- 前端jQuery之文档操作
1.文档操作内部插入 A.append(B) 吧B添加到A的后面 A.appendTo(B) 吧A添加到B的后面 A.prepend(B) 吧B添加到A的前面 A.prependTo(B) 吧A添加到 ...
- Hive[6] HiveQL 查询
6.1 SELECT ... FROM 语句 hive> SELECT name,salary FROM employees; --普通查询 hive>SELECT e.n ...
- 打包时ElementUI使vendor.js文件体量过大优化方法
<h1> 1.在index.html中以CDN的方式引入 </h1> <p> 引入的时候注意:要先在引入之前引入VUE否则会报undedined prototype ...
- LeetCode700. Search in a Binary Search Tree
题目 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜索树: 4 / \ 2 ...
- 【杂题总汇】NOIP2013(洛谷P1967) 货车运输
[洛谷P1967] 货车运输 重做NOIP提高组ing... +传送门-洛谷P1967+ ◇ 题目(copy from 洛谷) 题目描述 A国有n座城市,编号从1到n,城市之间有m条双向道路.每一条道 ...
- python简介,数据类型,input,if语句
1. python的起源 python的创始人为吉多·范罗苏姆(龟叔Guido van Rossum),1989年的圣诞节期间,龟叔为了在阿姆斯特丹打发时间 决心开发一个新的脚本程序解释器,作为A ...