Array Manipulation and Matrix Traversal Solutions

Array Increment Operation

Given a non-empty array representing a non-negative integer, increment the number by one. Each element stores a single digit, with the most significant digit at the head of the list.

class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        length = len(digits)
        # Traverse from rightmost digit
        for idx in range(length - 1, -1, -1):
            if digits[idx] != 9:
                digits[idx] += 1
                # Set all subsequent digits to zero
                for j in range(idx + 1, length):
                    digits[j] = 0
                return digits
        
        # All digits were 9, create new array
        return [1] + [0] * length

Find Pivot Index

Calculate the pivot index where the sum of elements to the left equals the sum of elements to the rightt.

class Solution:
    def pivotIndex(self, nums: List[int]) -> int:
        total_sum = sum(nums)
        left_sum = 0
        
        for i in range(len(nums)):
            # Check if left sum equals right sum
            if 2 * left_sum + nums[i] == total_sum:
                return i
            left_sum += nums[i]
        
        return -1

Array Rottaion

Rotate array elements to the right by k positions.

class Solution:
    def reverse_segment(self, arr: List[int], start: int, end: int) -> None:
        while start < end:
            arr[start], arr[end] = arr[end], arr[start]
            start += 1
            end -= 1
    
    def rotate(self, nums: List[int], k: int) -> None:
        n = len(nums)
        k %= n
        
        # Reverse entire array
        self.reverse_segment(nums, 0, n - 1)
        # Reverse first k elements
        self.reverse_segment(nums, 0, k - 1)
        # Reverse remaining elements
        self.reverse_segment(nums, k, n - 1)

Rotate Image

Rotate a square matrix 90 degrees clockwise in-place.

class Solution:
    def rotate(self, matrix: List[List[int]]) -> None:
        n = len(matrix)
        
        # Horizontal flip
        for i in range(n // 2):
            for j in range(n):
                matrix[i][j], matrix[n - i - 1][j] = matrix[n - i - 1][j], matrix[i][j]
        
        # Transpose along main diagonal
        for i in range(n):
            for j in range(i):
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

Spiral Matrix Traversal

Return all elements of a matrix in clockwise spiral order.

class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        if not matrix or not matrix[0]:
            return []
        
        rows, cols = len(matrix), len(matrix[0])
        result = []
        
        left, right, top, bottom = 0, cols - 1, 0, rows - 1
        
        while left <= right and top <= bottom:
            # Traverse right
            for col in range(left, right + 1):
                result.append(matrix[top][col])
            top += 1
            
            # Traverse down
            for row in range(top, bottom + 1):
                result.append(matrix[row][right])
            right -= 1
            
            # Traverse left
            if top <= bottom:
                for col in range(right, left - 1, -1):
                    result.append(matrix[bottom][col])
                bottom -= 1
            
            # Traverse up
            if left <= right:
                for row in range(bottom, top - 1, -1):
                    result.append(matrix[row][left])
                left += 1
        
        return result

Diagonal Traversal

Traverse matrix elements in diagonal order.

class Solution:
    def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
        rows, cols = len(mat), len(mat[0])
        result = []
        
        for diagonal in range(rows + cols - 1):
            if diagonal % 2 == 0:
                # Upward traversal
                start_row = min(rows - 1, diagonal)
                end_row = max(-1, diagonal - cols)
                for r in range(start_row, end_row, -1):
                    result.append(mat[r][diagonal - r])
            else:
                # Downward traversal
                start_row = max(0, diagonal - cols + 1)
                end_row = min(diagonal + 1, rows)
                for r in range(start_row, end_row):
                    result.append(mat[r][diagonal - r])
        
        return result

Tags: algorithms Arrays matrix traversal LeetCode

Posted on Tue, 01 Sep 2026 16:44:54 +0000 by jpt62089