• 剑指 Offer 29. 顺时针打印矩阵


    class Solution {
        public int[] spiralOrder(int[][] matrix) {
            int m = matrix.length;
            if(m == 0) return new int[]{};
            int n = matrix[0].length;
            if(m == 1) return matrix[0];
            if(n == 0) return new int[]{};
            int[] res = new int[m*n];
            int k = 0;
            int row_start=0,row_end = m,col_start = 0,col_end = n;
            while (row_start<row_end && col_start<col_end) {
                for (int i = col_start; i < col_end; i++) {
                    res[k++] = matrix[row_start][i];
                }
                if(k == m*n) return res;
                row_start++;
                for (int j = row_start; j < row_end; j++) {
                    res[k++] = matrix[j][col_end-1];
                }
                if(k == m*n) return res;
                col_end--;
                for (int i = col_end - 1; i >= col_start; i--) {
                    res[k++] = matrix[row_end-1][i];
                }
                if(k == m*n) return res;
                row_end--;
                for (int j = row_end - 1; j >= row_start; j--) {
                    res[k++] = matrix[j][col_start];
                }
                if(k == m*n) return res;
                col_start++;
            }
            return res;
        }
    }

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    python模拟shell
    10.LIKE 操作符
    9.TOP 子句--mysql limit
    8.INSERT INTO 语句 UPDATE 语句
    7.ORDER BY 子句
    6.AND & OR 运算符
    5.WHERE 子句
    4.SELECT DISTINCT 语句
    3.SELECT 语句
    2.sql分类
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/13471896.html
Copyright © 2020-2023  润新知