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
- Chapter 1APT
- 《sociology》chapter 1APT
- Chapter 1:IntroductionAPT
- Cognitive Neuroscience (Chapter 1)ROSAPT
- Chapter 1 線性表APT
- Chapter 1: Insurance BackgroundAPT
- Chapter 1 - Principles of InsuranceAPT
- Chapter 7 Resources in Plug-In(1)APT
- Chapter One————1、行前準備APT
- Chapter 1 First Sight——35APT
- Chapter 1 First Sight——22APT
- Microsoft Agent Tutorial Chapter 1 (轉)ROSAPT
- Effective Objective-C 2.0 Tips 總結 Chapter 1 & Chapter 2ObjectAPT
- Django 1.7 alpha 1 釋出Django
- Chapter1 入門/Section 1.2APT
- SG_007_CHAPTER 3(1)APT
- unp學習筆記——Chapter1筆記APT
- Arrays
- Chapter 1: Plug-in programing from past to the futureAPTAST
- delphi open arrays和dynamic arrays區別
- 資料結構與集合之(1)ArrayList 與 Arrays資料結構
- 程式設計師面試金典Chapter1程式設計師面試APT
- 【java】Arrays類Java
- Arrays工具類
- Laravel 專案:使用 TDD 構建論壇 Chapter 1LaravelAPT
- Computer Architecture A Quantitative Approach 讀書筆記chapter1-1APP筆記APT
- [Java] Introduction to Java Programming 筆記: Chapter 1. 概念Java筆記APT
- Java中Arrays作用Java
- Java Arrays.sort()Java
- java Arrays陣列Java陣列
- Chapter 4 圖APT
- Chapter 6 排序APT排序
- Analyzing Strings with sscanf
- Arrays.copyOf 函式函式
- Java容器工具類ArraysJava
- Swift-陣列(Arrays)Swift陣列
- JAVA基礎--Arrays類Java