/**
* Abstract: find the anchor and zero out its associate row&col, during
* which we collect the zero position when encountering one; after that
* we search the remaining entries for new anchors(which might be
* repeatedly enqueued).
*/
typedef enum Type {
TypeCol = 1 << 1,
TypeRow = 1 << 2,
TypeRowCol = TypeCol | TypeRow
};
typedef struct QNodeStruct {
int i, j;
enum Type type;
struct QNodeStruct *next;
}*QNode;
QNode QNodeCreate(int i, int j, enum Type type) {
QNode x = (QNode)malloc(sizeof(*x));
x->i = i;
x->j = j;
x->type = type;
x->next = NULL;
return x;
}
typedef struct QueueStruct {
int count;
QNode head, tail;
}*Queue;
Queue QueueCreate() {
Queue queue = (Queue)malloc(sizeof(*queue));
queue->count = 0;
queue->tail = queue->head = NULL;
return queue;
}
void QueueEnqueue(Queue queue, int i, int j, enum Type type) {
QNode head = QNodeCreate(i, j, type);
if (queue->count == 0) {
queue->head = queue->tail = head;
} else {
queue->head->next = head;
queue->head = head;
}
queue->count++;
}
void QueueDequeue(Queue queue, int *i, int *j, enum Type *type) {
QNode tail = queue->tail;
queue->tail = tail->next;
queue->count--;
*i = tail->i;
*j = tail->j;
*type = tail->type;
free(tail);
}
bool QueueIsEmpty(Queue queue) { return queue->count == 0; }
void set_zero(int **matrix, int m, int n, int row, int col, enum Type type, Queue queue, bool *rows, bool *cols) {
if ((type & TypeRow) && !(rows[row])) {
rows[row] = true;
for (int j = 0; j < col; j++) {
if (matrix[row][j] == 0) {
if (!(cols[j])) { QueueEnqueue(queue, row, j, TypeCol); }
} else {
matrix[row][j] = 0;
}
}
for (int j = col + 1; j < n; j++) {
if (matrix[row][j] == 0) {
if (!(cols[j])) { QueueEnqueue(queue, row, j, TypeCol); }
} else {
matrix[row][j] = 0;
}
}
}
if ((type & TypeCol) && !(cols[col])) {
cols[col] = true;
for (int i = 0; i < row; i++) {
if (matrix[i][col] == 0) {
if (!(rows[i])) { QueueEnqueue(queue, i, col, TypeRow); }
} else {
matrix[i][col] = 0;
}
}
for (int i = row + 1; i < m; i++) {
if (matrix[i][col] == 0) {
if (!(rows[i])) { QueueEnqueue(queue, i, col, TypeRow); }
} else {
matrix[i][col] = 0;
}
}
}
}
void find_next_zero(int **matrix, int m, int n, int row, Queue queue, bool *rows, bool *cols) {
for (int i = row; i < m; i++) {
if (!(rows[i])) {
for (int j = 0; j < n; j++) {
if (!(cols[j]) && matrix[i][j] == 0) {
QueueEnqueue(queue, i, j, TypeRowCol);
return;
}
}
}
}
}
void setZeroes(int** matrix, int matrixSize, int* matrixColSize) {
int m = matrixSize, n = *matrixColSize, row, col;
bool *rows = (bool*)malloc(m * sizeof(*rows)), *cols = (bool*)malloc(n * sizeof(*cols));
for (int i = 0; i < m; i++) { rows[i] = false; }
for (int j = 0; j < n; j++) { cols[j] = false; }
enum Type type;
Queue queue = QueueCreate();
find_next_zero(matrix, m, n, 0, queue, rows, cols);
while (!QueueIsEmpty(queue)) {
QueueDequeue(queue, &row, &col, &type);
set_zero(matrix, m, n, row, col, type, queue, rows, cols);
if (type == TypeRowCol) { find_next_zero(matrix, m, n, row + 1, queue, rows, cols); }
}
}
LeetCode #73 Set Matrix Zeroes
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門脑慧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來魄眉,“玉大人,你說我怎么就攤上這事闷袒】勇桑” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵囊骤,是天一觀的道長晃择。 經(jīng)常有香客問我,道長也物,這世上最難降的妖魔是什么宫屠? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮滑蚯,結果婚禮上浪蹂,老公的妹妹穿的比我還像新娘。我一直安慰自己膘魄,他們只是感情好乌逐,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著创葡,像睡著了一般浙踢。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上灿渴,一...
- 文/蒼蘭香墨 我猛地睜開眼倦零,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了吨悍?” 一聲冷哼從身側響起扫茅,我...
- 正文 年R本政府宣布驻子,位于F島的核電站灿意,受9級特大地震影響,放射性物質發(fā)生泄漏崇呵。R本人自食惡果不足惜缤剧,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望域慷。 院中可真熱鬧荒辕,春花似錦、人聲如沸犹褒。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽叠骑。三九已至李皇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間宙枷,已是汗流浹背掉房。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- LeetCode 73 Set Matrix Zeroes Given a m x n matrix, if an...
- 73 Set Matrix Zeroes 矩陣置零 Description:Given a m x n matri...
- 一、問題描述Given a m x n matrix, if an element is 0, set its e...
- 扯閑篇 為啥寫這個題锣夹? 因為這題由簡單到難坑真是多页徐。值得記錄下來好好研究研究 題目 Given amxnmatri...
- 文章作者:Tyan博客:noahsnail.com | CSDN | 簡書 1. Description 2. S...