算法练习--LeetCode--54. Spiral Matrix 100%

  1. Spiral Matrix
    Medium

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]

Example 2:

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

简单说就是二维数组顺时针🔃读取成一位数组,拆开看问题,其实每次去掉最外面一层剩下的还是一个二维数组,从这里出发可以使用递归,但是这里使用循环就够了,加四个标志位:上下左右,Swift(100%)代码如下:

// swift code
class Solution {
    func spiralOrder(_ matrix: [[Int]]) -> [Int] {
        // 特殊 case 直接返回
        if matrix.count < 2 { return matrix.first ?? [] }

        // 标记左右上下
        var l = 0, r = matrix.first!.count - 1, t = 0, b = matrix.count - 1
        var result = [Int]()

        // 数组片段转置且类型转为数组
        let reverse_: (ArraySlice<Int>) -> [Int] = { (slice) in
            var temp = slice
            temp.reverse()
            return Array(temp)
        }

        while true {

            // 每一组的第一行是可以直接追加到结果中的
            result += Array(matrix[t][l...r])

            if t + 1 < b {
                // 自上至下取出每一个数组的最后一个有效元素加入到结果
                for i in (t + 1)..<b {
                    result.append(matrix[i][r])
                }
                result += reverse_(matrix[b][l...r])
                if l != r {
                    var temp = b - 1

                    // 自下至上取出每一个数组的第一个有效元素加入到结果
                    while temp > t {
                        result.append(matrix[temp][l])
                        temp -= 1
                    }
                    r -= 1
                    l += 1
                    t += 1
                    b -= 1
                    if l > r {
                        break
                    }
                } else {
                    break
                }
            } else if t != b {
                // 最后一行的转置数组加到结果中
                result += reverse_(matrix[b][l...r])
                break
            } else {
                break
            }
        }
        return result
    }
}

TestCase

// swift code
assert(Solution().spiralOrder([[1]]) == [1])
assert(Solution().spiralOrder([[1], [2]]) == [1, 2])
assert(Solution().spiralOrder([[1], [2], [3]]) == [1, 2, 3])
assert(Solution().spiralOrder([[1, 2], [4, 5]]) == [1, 2, 5, 4])
assert(Solution().spiralOrder([[2, 5], [8, 4], [0 ,-1]]) == [2, 5, 4, -1, 0, 8])
assert(Solution().spiralOrder([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) == [1, 2, 3, 6, 9, 8, 7, 4, 5])
assert(Solution().spiralOrder([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) == [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7])
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • <center>#1 Two Sum</center> link Description:Given an arr...
    铛铛铛clark阅读 2,196评论 0 3
  • 年少时的我们,总是仰望 曾努力盼着可以记下 长大后,我们忘记 长大后,我们遗弃 星空 月光 彩霞 已不见时 长大了
    腿毛根根向北吹阅读 137评论 0 0
  • 我从不会在上课的时候, 把任何一个学生赶出教室。 偶尔, 会让课堂上特别调皮的, "罚"站一会子 过一会儿,又舍不...
    麦田里的守护天使阅读 486评论 0 2
  • app 应用文件Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录tmp:存...
    HAKA阅读 121评论 0 0
  • 谁出现在你梦里,谁陪你疯掉整场青春。远去的背影,时光模糊不了你的身影,淡不了你的容颜。风轻轻的吹,拂过我的脸庞,就...
    安晓晓阅读 429评论 0 0