leetcode -- 73. Set Matrix Zeroes

2024-04-13 01:32
文章标签 leetcode set 73 matrix zeroes

本文主要是介绍leetcode -- 73. Set Matrix Zeroes,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述

题目难度:Medium
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.

  • Example 1:

Input:
[
[1,1,1],
[1,0,1],
[1,1,1]
]
Output:
[
[1,0,1],
[0,0,0],
[1,0,1]
]

  • Example 2:

Input:
[
[0,1,2,0],
[3,4,5,2],
[1,3,1,5]
]
Output:
[
[0,0,0,0],
[0,4,5,0],
[0,3,1,0]
]
Follow up:

A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

AC代码1

Complexity Analysis

Time Complexity: O(M \times N)O(M×N) where M and N are the number of rows and columns respectively.

Space Complexity: O(M + N)O(M+N).

class Solution {public void setZeroes(int[][] matrix) {Set<Integer> rowSet = new HashSet<>();Set<Integer> colSet = new HashSet<>();int m = matrix.length;int n = matrix[0].length;for(int i = 0;i < m;i++){for(int j = 0;j < n;j++){if(matrix[i][j] == 0){rowSet.add(i);colSet.add(j);}}}setRowZeros(matrix, rowSet, n);setColZeros(matrix, colSet, m);}private void setRowZeros(int[][] matrix, Set<Integer> set, int n){for(Integer i : set)for(int j = 0;j < n;j++) matrix[i][j] = 0;}private void setColZeros(int[][] matrix, Set<Integer> set, int m){for(Integer i : set)for(int j = 0;j < m;j++) matrix[j][i] = 0;}
}

AC代码2

Complexity Analysis

Time Complexity : O(M + N)(M+N).
Space Complexity : O(1)

class Solution {public void setZeroes(int[][] matrix) {int MODIFIED = -1000000;int R = matrix.length;int C = matrix[0].length;for (int r = 0; r < R; r++) {for (int c = 0; c < C; c++) {if (matrix[r][c] == 0) {// We modify the corresponding rows and column elements in place.// Note, we only change the non zeroes to MODIFIEDfor (int k = 0; k < C; k++) {if (matrix[r][k] != 0) {matrix[r][k] = MODIFIED;}}for (int k = 0; k < R; k++) {if (matrix[k][c] != 0) {matrix[k][c] = MODIFIED;}}}}}for (int r = 0; r < R; r++) {for (int c = 0; c < C; c++) {// Make a second pass and change all MODIFIED elements to 0 """if (matrix[r][c] == MODIFIED) {matrix[r][c] = 0;}}}}
}

AC代码3

Complexity Analysis

Time Complexity : O(M×N)
Space Complexity : O(1)

class Solution {public void setZeroes(int[][] matrix) {Boolean isCol = false;int R = matrix.length;int C = matrix[0].length;for (int i = 0; i < R; i++) {// Since first cell for both first row and first column is the same i.e. matrix[0][0]// We can use an additional variable for either the first row/column.// For this solution we are using an additional variable for the first column// and using matrix[0][0] for the first row.if (matrix[i][0] == 0) {isCol = true;}for (int j = 1; j < C; j++) {// If an element is zero, we set the first element of the corresponding row and column to 0if (matrix[i][j] == 0) {matrix[0][j] = 0;matrix[i][0] = 0;}}}// Iterate over the array once again and using the first row and first column, update the elements.for (int i = 1; i < R; i++) {for (int j = 1; j < C; j++) {if (matrix[i][0] == 0 || matrix[0][j] == 0) {matrix[i][j] = 0;}}}// See if the first row needs to be set to zero as wellif (matrix[0][0] == 0) {for (int j = 0; j < C; j++) {matrix[0][j] = 0;}}// See if the first column needs to be set to zero as wellif (isCol) {for (int i = 0; i < R; i++) {matrix[i][0] = 0;}}}
}

这篇关于leetcode -- 73. Set Matrix Zeroes的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/898783

相关文章

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

poj 3050 dfs + set的妙用

题意: 给一个5x5的矩阵,求由多少个由连续6个元素组成的不一样的字符的个数。 解析: dfs + set去重搞定。 代码: #include <iostream>#include <cstdio>#include <set>#include <cstdlib>#include <algorithm>#include <cstring>#include <cm

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

Collection List Set Map的区别和联系

Collection List Set Map的区别和联系 这些都代表了Java中的集合,这里主要从其元素是否有序,是否可重复来进行区别记忆,以便恰当地使用,当然还存在同步方面的差异,见上一篇相关文章。 有序否 允许元素重复否 Collection 否 是 List 是 是 Set AbstractSet 否

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param

论文翻译:ICLR-2024 PROVING TEST SET CONTAMINATION IN BLACK BOX LANGUAGE MODELS

PROVING TEST SET CONTAMINATION IN BLACK BOX LANGUAGE MODELS https://openreview.net/forum?id=KS8mIvetg2 验证测试集污染在黑盒语言模型中 文章目录 验证测试集污染在黑盒语言模型中摘要1 引言 摘要 大型语言模型是在大量互联网数据上训练的,这引发了人们的担忧和猜测,即它们可能已