题解 | #螺旋矩阵#

螺旋矩阵

https://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31

import java.util.*;


public class Solution {
    public ArrayList<Integer> spiralOrder (int[][] matrix) {
        // write code here
        ArrayList<Integer> res = new ArrayList<>();
        if(matrix.length == 0)
            return res;
        // 定义四个指针,并且充当边界限制的作用
        int top = 0, bottom = matrix.length-1;
        int left = 0, right = matrix[0].length-1;
       
        while(true){
            //上面  左到右
            for(int i = left; i <= right; i++){
                res.add(matrix[top][i]);
            } ++top;
            if(top > bottom) break;
 
            //右边 上到下
            for(int i = top; i <= bottom; i++){
                res.add(matrix[i][right]);
            }--right;
            if(right < left) break;
 
            //下面  右到左
            for(int i = right; i>=left; i--){
                res.add(matrix[bottom][i]);
            }--bottom;
            if(bottom < top) break;
 
            //左边 下到上
            for(int i = bottom; i>=top; i--){
                res.add(matrix[i][left]);
            }left++;
            if(left > right) break;           
        }
        return res;
    }
}

全部评论

相关推荐

07-03 11:02
中山大学 C++
字节刚oc,但距离九月秋招很近了有两段互联网实习,非腾讯字节。不敢赌转正,现在在纠结去还是不去如果实习俩月离职会有什么后果吗
阿城我会做到的:不去后悔一辈子,能否转正取决于ld的态度,只要他不卡,答辩就是走流程,个人觉得可以冲一把
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务