-
[75 LeetCode] 7 - Set Matrix ZeroesStudy/Leetcode 2023. 4. 8. 17:33
[75 LeetCode]는 코딩테스트 연습을 위해 한 페이스북 개발자가 추천하는 75가지 알고리즘 문제를 풀어보는 시리즈이다.
블라인드 원문:
https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-75-LeetCode-Questions-to-Save-Your-Time-OaM1orEU문제 링크: https://leetcode.com/problems/set-matrix-zeroes/
Set Matrix Zeroes - LeetCode
Can you solve this real interview question? Set Matrix Zeroes - Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's. You must do it in place [https://en.wikipedia.org/wiki/In-place_algorithm]. Example 1: [https
leetcode.com
matrix에 대해 in-place로 해당 과제를 수행해야한다.
즉 간단히 0을 다 찾고, 찾은 0의 행과 열에 대해서 0으로 만들기를 수행한다.function setZeroes(matrix: number[][]): void { const rowVisited = new Map(); const colVisited = new Map(); for(let i = 0; i < matrix.length; i++) { for( let j = 0; j < matrix[0].length; j ++) { if (matrix[i][j] === 0) { rowVisited.set(i, true) colVisited.set(j, true) } } } for (let i of rowVisited.keys()) { for( let j = 0; j < matrix[0].length; j ++) { matrix[i][j] = 0; } } for (let i of colVisited.keys()) { for( let j = 0; j < matrix.length; j ++) { matrix[j][i] = 0; } } };
728x90'Study > Leetcode' 카테고리의 다른 글
[75 LeetCode] 9 - Maximum Depth of Binary Tree (0) 2023.04.26 [75 LeetCode] 8 - Longest Substring Without Repeating Characters (0) 2023.04.08 [75 LeetCode] 6 - Reverse Linked List (0) 2023.04.08 [75 LeetCode] 5 - Insert Interval (0) 2023.04.05 [75 LeetCode] 4 - Clone Graph (0) 2023.04.05