Chapter 1 Arrays and Strings - 1.7
Problem 1.7: Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
At the first glance, we can just iterate the matrix and when we find a zero, we set the whole row and column to be zero. However, if we write down a simple instance to verify the solution, we can find it's totally wrong. Changing the matrix is not reasonable because we need to differentiate zeros in original matrix from zeros we set.
The second idea is having MXN flags and mark the positions that should be set to zeros. This one is desirable, but not the best one, because it takes O(MXN) space, which can be reduced.
A better solution is given below:
Lesson: write down simple instance to verify the solution and it will prevent naive mistakes.
At the first glance, we can just iterate the matrix and when we find a zero, we set the whole row and column to be zero. However, if we write down a simple instance to verify the solution, we can find it's totally wrong. Changing the matrix is not reasonable because we need to differentiate zeros in original matrix from zeros we set.
The second idea is having MXN flags and mark the positions that should be set to zeros. This one is desirable, but not the best one, because it takes O(MXN) space, which can be reduced.
A better solution is given below:
def set_zeros(Mat, M, N):
row_flags = [False for i in range(0, M)]
col_flags = [False for i in range(0, N)]
# Mark each row and column that has zero
for i in range(0, M):
for j in range(0, N):
if Mat[i][j] == 0:
row_flags[i] = True
col_flags[j] = True
# Set zeros of each marked row
for i in range(0, M):
if row_flags[i] == True:
for j in range(0, N):
Mat[i][j] = 0
# Set zeros of each marked column
for j in range(0, N):
if col_flags[j] == True:
for i in range(0, M):
Mat[i][j] = 0
# Test case
if __name__ == "__main__":
Mat = [[0, 0, 2, 3, 4], [4, 5, 6, 7, 8], [9, 1, 1, 2, 0]]
set_zeros(Mat, 3, 5)
for i in range(0, len(Mat)):
for j in range(0, len(Mat[i])):
print Mat[i][j], " ",
print "\n"
Lesson: write down simple instance to verify the solution and it will prevent naive mistakes.
相關文章
- Chapter 5:Perl One-Liners:Working with Arrays and StringsAPT
- Cognitive Neuroscience (Chapter 1)ROSAPT
- Chapter 1 線性表APT
- Chapter One————1、行前準備APT
- Chapter 7 Resources in Plug-In(1)APT
- sky-take-out chapter 1APT
- [TinyRenderer] Chapter1 p1 Output ImageAPT
- Chapter1 入門/Section 1.2APT
- Chapter 1: Plug-in programing from past to the futureAPTAST
- [TinyRenderer] Chapter1 p3 LineAPT
- [TinyRenderer] Chapter1 p2 vecAPT
- Arrays
- Arrays Basics
- 六,Arrays
- [Java] Introduction to Java Programming 筆記: Chapter 1. 概念Java筆記APT
- 資料結構與集合之(1)ArrayList 與 Arrays資料結構
- 1.7
- Encode and Decode Strings
- no-strings-attached
- 程式設計師面試金典Chapter1程式設計師面試APT
- Laravel 專案:使用 TDD 構建論壇 Chapter 1LaravelAPT
- Arrays工具類
- Arrays+Collections
- 【java】Arrays類Java
- Add Strings 字串相加字串
- strings 和 strconv 包
- Go Standard library - stringsGo
- CSAPP英語學習系列:Chapter 1: A Tour of Computer Systems.APPAPT
- Sorting arrays in NumPy by column
- Java中Arrays作用Java
- 18_Arrays類
- Java Arrays.sort()Java
- chapter18APT
- chapter4APT
- chapter14APT
- chapter5APT
- chapter7APT
- Chapter 6 排序APT排序
- Chapter 4 圖APT