讨论/《画解剑指 Offer》 - 剑指 Offer 29. 顺时针打印矩阵 - 解决方案/
《画解剑指 Offer》 - 剑指 Offer 29. 顺时针打印矩阵 - 解决方案
共 5 个回复

贴个js

/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
/**
 * [1, 2, 3]
 * [4, 5, 6]
 * [7, 8, 9]
 * 
 * [ 1,  2,  3,  4]
 * [ 5,  6,  7,  8]
 * [ 9, 10, 11, 12]
 * 
 */
var spiralOrder = function(matrix) {
    let row = 0, col = 0;
    let direction = "right", border = 0;    //direction是当前遍历方向, border是边界
    let sum = 0, n = 0;                     //sum是矩阵中元素总数, n是当前遍历的元素数量
    let arr = [];                           //返回的数组
    for(let i = 0; i < matrix.length; ++i)
    {
        sum += matrix[i].length;
    }
    while(n < sum)
    {
        if(direction == 'right')
        {
            if(col <= matrix[0].length - 1 - border)
            {
                arr.push(matrix[row][col++]);
                ++n;
            }
            else
            {
                --col;
                ++row;
                direction = 'down';
            }
        }
        else if(direction == 'down')
        {
            if(row <= matrix.length - 1 - border)
            {
                arr.push(matrix[row++][col]);
                ++n;
            }
            else
            {
                --row;
                --col;
                direction = 'left';
            }
        }
        else if(direction == 'left')
        {
            if(col >= 0 + border)
            {
                arr.push(matrix[row][col--]);
                ++n;
            }
            else
            {
                ++col;
                --row;
                direction = 'up';
                ++border;
            }
        }
        else if(direction == 'up')
        {
            if(row >= 0 + border)
            {
                arr.push(matrix[row--][col]);
                ++n;
            }
            else
            {
                ++row;
                ++col;
                direction = 'right';
            }
        }
    }
    return arr;
};
1

贴个C++

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
    vector<int> result;
    if(matrix.empty()) return result;
    int top = 0;int button = matrix.size();int left = 0;int right = matrix[0].size();
    
    
    while(true) {
        //按顺序循环
        for(int i = left;i< right;++i) result.push_back(matrix[top][i]);
        if(++top >= button) break;
        for(int i = top;i <button;++i) result.push_back(matrix[i][right-1]);
        if(--right <= left) break;
        for(int i = right-1;i >= left;--i) result.push_back(matrix[button-1][i]);
        if(--button <= top)break;
        for(int i = button-1; i >= top;--i) result.push_back(matrix[i][left]);
        if(++left >= right) break;
    }
    return result;
    }
};
1
public class Solution {
    private int[] res;

    public int[] spiralOrder(int[][] matrix) {
        // 注意:  int[][] matrix = {{}}
        // matrix.length=1,matrix[0].length=0
        // 输入空列,返回空数组
        if (matrix.length == 0) {
            return new int[0];
        }
        // 初始化左上角、右下角坐标
        int tR = 0, tC = 0;
        int dR = matrix.length - 1, dC = matrix[0].length - 1;
        res = new int[matrix.length * matrix[0].length];
        int index = 0;
        while (tR <= dR && tC <= dC) {
            index = spiralMatrix(matrix, index, tR++, tC++, dR--, dC--);
        }
        return res;
    }

    private int spiralMatrix(int[][] matrix, int index, int tR, int tC, int dR, int dC) {
        if (tR == dR) {// 子矩阵只有一行
            for (int i = tC; i <= dC; i++) {
                res[index++] = matrix[tR][i];
            }
        } else if (tC == dC) {// 子矩阵只有一列
            for (int i = tR; i <= dR; i++) {
                res[index++] = matrix[i][tC];
            }
        } else {
            int curR = tR;
            int curC = tC;
            while (curC != dC) {
                res[index++] = matrix[tR][curC++];
            }
            while (curR != dR) {
                res[index++] = matrix[curR++][dC];
            }
            while (curC != tC) {
                res[index++] = matrix[dR][curC--];
            }
            while (curR != tR) {
                res[index++] = matrix[curR--][tC];
            }
        }
        return index;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
//        int[][] matrix = {{}};
//        System.out.println(matrix.length);
//        System.out.println(matrix[0].length);
        int[] res = solution.spiralOrder(matrix);
        System.out.println(Arrays.toString(res));
    }
}

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0){
            int[] a=new int[]{};
             return a;
        }
           
        int l = 0;
        int r = matrix[0].length;
        int u = 0;
        int d = matrix.length;
        int m=0;
        int[] array=new int[r*d];
        r--;
        d--;
        while (l <= r && u <= d){
            for (int i = l; i <= r; i++) {
                array[m]=matrix[u][i];
                m++;
            }
            u++;
            for (int i = u; i <= d; i++) {
                array[m]=matrix[i][r];
                m++;
            }
            r--;
            for (int i = r; i >= l && u <= d; i--) {
                array[m]=matrix[d][i];
                m++;
            }
            d--;
            for (int i = d; i >= u && l <= r; i--) {
                array[m]=matrix[i][l];
                m++;
            }
            l++;
        }
        return array;               
    }
}
class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        help = []
        if matrix == []:
            return []
        left, right, top, buttom = 0, len(matrix[0]) - 1, 0, len(matrix) - 1
        while True:
            for i in range(left, right + 1):
                help.append(matrix[top][i])
            top += 1
            if top > buttom:
                break
            for i in range(top, buttom + 1):
                help.append(matrix[i][right])
            right -= 1
            if right < left:
                break
            for i in range(right, left - 1, -1):
                help.append(matrix[buttom][i])
            buttom -= 1
            if buttom < top:
                break
            for i in range(buttom, top - 1, -1):
                help.append(matrix[i][left])
            left += 1
            if left > right:
                break
        return help