diff --git a/set-matrix-zeroes/evan.py b/set-matrix-zeroes/evan.py new file mode 100644 index 000000000..49bb846dd --- /dev/null +++ b/set-matrix-zeroes/evan.py @@ -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 diff --git a/spiral-matrix/evan.py b/spiral-matrix/evan.py new file mode 100644 index 000000000..6eb922b01 --- /dev/null +++ b/spiral-matrix/evan.py @@ -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 diff --git a/sum-of-two-integers/evan.py b/sum-of-two-integers/evan.py new file mode 100644 index 000000000..15ca8b676 --- /dev/null +++ b/sum-of-two-integers/evan.py @@ -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