Leetcode - Two Sum II - Input array is sorted
Leetcode - Spiral Matrix II

Leetcode - spiral-matrix

violet posted @ Mar 03, 2020 08:59:03 AM in 算法 with tags Algorithm 算法 array Golang ruby , 551 阅读

https://leetcode.com/problems/spiral-matrix/

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

Example 1:

Input:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]

 

Follow the direction, right -> down -> left -> up, use rowStart, rowEnd, columnStart, columnEnd for the each direction. After each direction, it should update rowStart, rowEnd, columnStart, columnEnd as well. Use visited to count visited element. Once visited is equal to total number, break.

func spiralOrder(matrix [][]int) []int {
    if len(matrix) == 0 || len(matrix[0]) == 0 {
        return []int{}
    }
    directions := []string{"right", "down", "left", "up"}
    rowStart := 0
    rowEnd := len(matrix)
    columnStart := 0
    columnEnd := len(matrix[0])
    result := []int{}
    total := len(matrix) * len(matrix[0])
    visited := 0
    for visited != total {
        for _, d := range directions {
            if d == "right" {
                for i := columnStart; i < columnEnd; i++ {
                    result = append(result, matrix[rowStart][i])
                    visited++
                }
                rowStart++
            }
            if d == "down" {
                for i := rowStart; i < rowEnd; i++ {
                    result = append(result, matrix[i][columnEnd-1])
                    visited++
                }
                columnEnd--
            }
            if d == "left" {
                for i := columnEnd-1; i >= columnStart; i-- {
                    result = append(result, matrix[rowEnd-1][i])
                    visited++
                }
                rowEnd--
            }
            if d == "up" {
                for i := rowEnd-1; i >= rowStart; i-- {
                    result = append(result, matrix[i][columnStart])
                    visited++
                }
                columnStart++
            }
  
            if (rowStart == rowEnd && columnStart == columnEnd) || (visited == total) {
                break
            }
        }
        if rowStart == rowEnd && columnStart == columnEnd {
            break
        }
    }
    
    return result
}

 

# @param {Integer[][]} matrix
# @return {Integer[]}
def spiral_order(matrix)
    return [] if matrix.length == 0 || matrix[0].length == 0
    r = matrix.length
    c = matrix[0].length
    row_start = 0
    row_end = r - 1
    column_start = 0
    column_end = c - 1
    result = []
    total = r * c
    visited = 0
    directions = ["right", "down", "left", "up"]
    while visited != total
        directions.each do |d|
            if d == "right"
               for i in column_start..column_end do
                  result << matrix[row_start][i]
                  visited += 1
                end
                row_start += 1
            end
            if d == "down"
                for i in row_start..row_end do
                   result << matrix[i][column_end]
                   visited += 1
                end
                column_end -= 1
            end
            if d == "left"
                i = column_end
                while i >= column_start
                    result << matrix[row_end][i]
                    visited += 1
                    i -= 1
                 end
                row_end -= 1
            end
            if d == "up"
                i = row_end
                while i >= row_start
                    result << matrix[i][column_start]
                    visited += 1
                    i -= 1
                end
                column_start += 1
            end
            if (row_start == row_end && column_start == column_end) || visited == total
                break
            end
            if row_start == row_end && column_start == column_end
                break
            end
        end
    end
    return result
end
VIP Escorts in Delhi 说:
Feb 03, 2022 02:49:03 PM

Brilliant and great work has been done out here in nice and effective ways.

AP 10th Evs Model Pa 说:
Sep 19, 2022 12:59:37 AM

Advised to everyone can contact the class teacher to get important questions for all lessons and topics of EVS. Every Telugu Medium, English Medium and Urdu Medium student of the State Board can download the AP 10th Class EVS Model Paper 2023 Pdf with answers for term-1 & term-2 exams of SA-1, SA-2 and other exams of the board. AP 10th Evs Model Paper Environmental Education is one of the most important subjects and it’s a part of Science. School Education Department and various leading private school teaching staff have designed and suggested the practice question paper for all Part-A, Part-B, Part-C, and Part-D questions of SA-1, SA-2, FA-1, FA-2, FA-3, FA-4 and Assignments.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter