Skip to content

Evan Week 14 #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions set-matrix-zeroes/evan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import List


class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
if not matrix or not matrix[0]:
return

rows, cols = len(matrix), len(matrix[0])
first_row_has_zero = any(matrix[0][j] == 0 for j in range(cols))
first_col_has_zero = any(matrix[i][0] == 0 for i in range(rows))

# Use the first row and first column to mark zeros
for i in range(1, rows):
for j in range(1, cols):
if matrix[i][j] == 0:
matrix[0][j] = 0
matrix[i][0] = 0

# Set matrix elements to zero based on marks
for i in range(1, rows):
for j in range(1, cols):
if matrix[0][j] == 0 or matrix[i][0] == 0:
matrix[i][j] = 0

# Set the first row to zero if needed
if first_row_has_zero:
for j in range(cols):
matrix[0][j] = 0

# Set the first column to zero if needed
if first_col_has_zero:
for i in range(rows):
matrix[i][0] = 0
40 changes: 40 additions & 0 deletions spiral-matrix/evan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import List


class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if not matrix or not matrix[0]:
return []

result = []
left, right = 0, len(matrix[0]) - 1
top, bottom = 0, len(matrix) - 1

while left <= right and top <= bottom:
# Traverse from left to right
for i in range(left, right + 1):
result.append(matrix[top][i])
# Move the top boundary down
top += 1

# Traverse from top to bottom
for i in range(top, bottom + 1):
result.append(matrix[i][right])
# Move the right boundary to the left
right -= 1

if top <= bottom:
# Traverse from right to left
for i in range(right, left - 1, -1):
result.append(matrix[bottom][i])
# Move the bottom boundary up
bottom -= 1

if left <= right:
# Traverse from bottom to top
for i in range(bottom, top - 1, -1):
result.append(matrix[i][left])
# Move the left boundary to the right
left += 1

return result
23 changes: 23 additions & 0 deletions sum-of-two-integers/evan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
def getSum(self, a: int, b: int) -> int:
# 32비트 마스크
MASK = 0xFFFFFFFF
# 최대 32비트 정수 값
MAX_INT = 0x7FFFFFFF

while b != 0:
# a와 b의 XOR (덧셈 결과)
a_xor_b = (a ^ b) & MASK
# 자리 올림 (AND 연산 후 한 비트 왼쪽 시프트)
carry = ((a & b) << 1) & MASK

# a를 XOR 결과로 업데이트
a = a_xor_b
# b를 자리 올림 값으로 업데이트
b = carry

if a > MAX_INT:
# 음수 처리
a = ~(a ^ MASK)

return a