Life is a fucking movie.|

Ac_c0mpany丶

园龄:3年7个月粉丝:6关注:3

2023-12-06 11:39阅读: 3评论: 0推荐: 0

[LeetCode Hot 100] LeetCode73. 矩阵置零

题目描述

思路一:开辟两个数组,时间复杂度O(m + n)

开辟两个数组用来记录哪些行、哪些列需要置为零。
这样时间复杂度为O(m + n)。

思路二:

原地算法:不适用额外空间或者说常数级空间来实现算法。
类似于使用set保存每行每列是否需要置零,

方法一:对应思路一

class Solution {
    public void setZeroes(int[][] matrix) {
        // m代表行,n代表列
        int m = matrix.length;
        int n = matrix[0].length;

        Set<Integer> row = new HashSet<>();
        Set<Integer> col = new HashSet<>();

        for (int i = 0; i < m; i ++) {
            for (int j = 0; j < n; j ++) {
                if (matrix[i][j] == 0) {
                    row.add(i);
                    col.add(j);
                }
            }
        }

        for (int i = 0; i < m; i ++) {
            for (int j = 0; j < n; j ++) {
                if (row.contains(i) || col.contains(j)) {
                    matrix[i][j] = 0;
                }
            }
        }
    }
}

方法二:对应思路2

class Solution {
    public void setZeroes(int[][] matrix) {
        // 矩阵的行记为row,矩阵的列记为col
        int row = matrix.length, col = matrix[0].length;
        // 弄两个标记位,记录第0行和第0列是否有0
        boolean rowZero = false, colZero = false;

        // 遍历矩阵
        for (int i = 0; i < row; i ++) {
            for (int j = 0; j < col; j ++) {
                if (matrix[i][j] == 0) {
                    matrix[0][j] = matrix[i][0] = 0;
                    if (i == 0) rowZero = true;
                    if (j == 0) colZero = true;
                }
            }
        } 

        // 除开第一行第一列, 将矩阵置0
        for (int i = 1; i < row; i ++) {
            for (int j = 1; j < col; j ++) {
                if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                    matrix[i][j] = 0;
                }
            }
        }

        for (int i = 0; colZero == true && i < row; i ++) matrix[i][0] = 0;
        for (int j = 0; rowZero == true && j < col; j ++) matrix[0][j] = 0;
    }
}

本文作者:keyongkang

本文链接:https://www.cnblogs.com/keyongkang/p/17879171.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ac_c0mpany丶  阅读(3)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 You Are My Sunshine REOL
You Are My Sunshine - REOL
00:00 / 00:00
An audio error has occurred.

作曲 : Traditional

You are my sunshine

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away

The other night, dear,

When I lay sleeping

I dreamed I held you in my arms.

When I awoke, dear,

I was mistaken

So I hung my head and cried.

You are my sunshine,

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away.

You are my sunshine,

My only sunshine

You make me happy

When skies are gray.

You'll never know, dear

How much I love you

Please don't take my sunshine away

Please don't take my sunshine away.

Please don't take my sunshine away.