매트릭스 0 설정
'행렬 0 설정' 문제에 대한 자세한 가이드 및 Python 구현입니다.
1. 배우다
'행렬 0 설정' 문제는 수학 및 기하학 섹션의 주요 과제입니다.
이 구현은 Python의 쉬운 수준 논리에 중점을 둡니다.
우리는 제공되는 솔루션에서 기술적 정확성과 코드 가독성을 최우선으로 생각합니다.
2. Real-World Applications
3. Visual Intuition
Set Matrix Zeroes에 대한 논리 흐름을 시각화합니다.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Set Matrix Zeroes에 대한 문제 설명을 주의 깊게 읽어보세요.
2. Formulate brute force
간단한 반복 솔루션 초안을 작성합니다.
3. Identify inefficiency
중복 계산을 찾으십시오.
4. Optimize search path
해싱이나 정렬을 사용하여 프로세스 속도를 높입니다.
5. Final Implementation
생산 표준에 맞게 코드를 정리합니다.
문제 설명
m x n 정수 행렬 행렬이 주어졌을 때 요소가 0이면 전체 행과 열을 0으로 설정합니다.
그 자리에서 하셔야 합니다.
행렬을 제자리에서 수정하고 반환하는 setZeroes(matrix: list) -> list 함수를 구현하세요.
- •m == matrix.length
- •n == matrix[0].length
- •1 <= m, n <= 200
- •-2^31 <= matrix[i][j] <= 2^31 - 1
예
[[1,1,1],[1,0,1],[1,1,1]]
[[1,0,1],[0,0,0],[1,0,1]]
The element at position (1,1) is 0. So the entire row 1 and column 1 are set to 0.
[[0,1,2,0],[3,4,5,2],[1,3,1,5]]
[[0,0,0,0],[0,4,5,0],[0,3,1,0]]
Elements at (0,0) and (0,3) are 0. Row 0 becomes all zeros. Columns 0 and 3 become all zeros.
Need a Hint?
Edge Cases to Watch
- 빈 입력 구조
- 단일 요소 입력
- 큰 수치 범위
해결할 준비가 되셨나요?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
인터뷰 통찰력 및 변형
복잡성 분석 분석
왜 시간인가?: Directly evaluates all possibilities.
왜 우주인가?: Uses standard local memory.
왜 시간인가?: Optimized paths reduce total operations.
왜 우주인가?: May trade memory for speed.
최적화된 솔루션 Python 코드
최적화된 솔루션 Python 코드
def set_zeroes_opt(matrix: list[list[int]]) -> None:
rows, cols = len(matrix), len(matrix[0])
row_zero = False
for r in range(rows):
for c in range(cols):
if matrix[r][c] == 0:
matrix[0][c] = 0
if r > 0:
matrix[r][0] = 0
else:
row_zero = True
for r in range(1, rows):
for c in range(1, cols):
if matrix[0][c] == 0 or matrix[r][0] == 0:
matrix[r][c] = 0
if matrix[0][0] == 0:
for r in range(rows):
matrix[r][0] = 0
if row_zero:
for c in range(cols):
matrix[0][c] = 0무차별 대입 코드(스포일러 보호)
무차별 대입 코드(스포일러 보호)
def set_zeroes_brute(matrix: list[list[int]]) -> None:
rows, cols = len(matrix), len(matrix[0])
row_zero = set()
col_zero = set()
for r in range(rows):
for c in range(cols):
if matrix[r][c] == 0:
row_zero.add(r)
col_zero.add(c)
for r in range(rows):
for c in range(cols):
if r in row_zero or c in col_zero:
matrix[r][c] = 0Algorithm Pattern Checklist
When dealing with Math & Geometry data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
Key Revision Notes
표준 수학 및 기하학 문제 속성이 적용됩니다.
관련 질문
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
권장 Python 리소스
관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.
Python 세트
Python의 마스터 세트. 고유한 값을 저장하고, 교차점, 합집합, 차이점을 실행하고, 해싱의 성능 이점을 이해하는 방법을 알아보세요.
Python에서 목록을 정렬하는 방법(오름차순 및 내림차순)
sort() 메서드와 sorted() 함수를 사용하여 Python에서 목록을 정렬하는 방법을 알아보세요. 사용자 정의 키 정렬 및 역순 예시를 살펴보세요.
Python 설정 방법 치트 시트
Python 집합 작업에 대한 전체 가이드입니다. 합집합 및 교차점과 같은 수학적 집합 연산을 추가, 제거 및 수행하는 방법을 알아보세요.
Python 대 JavaScript: 어떤 프로그래밍 언어가 가장 좋나요?
Python과 JavaScript를 포괄적으로 비교합니다. 구문 차이점, 성능, 사용 사례(백엔드와 프런트엔드) 및 코딩 예제를 살펴보세요.