题解 | #螺旋矩阵#

螺旋矩阵

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

import java.util.*;
public class Solution {
    public ArrayList<Integer> spiralOrder(int[][] matrix) {

        ArrayList<Integer> record=new ArrayList<Integer>();
        if(matrix==null||matrix.length==0)return record;
        int bottom=matrix.length-1;
        int right=matrix[0].length-1;
        int top=0;
        int left=0;
        while(top<=bottom&&left<=right){//只有一行或者只有一列也要进行循环啊
            for(int i=left;i<=right;i++){
                record.add(matrix[top][i]);
            }
            for(int i=top+1;i<=bottom;i++){
                record.add(matrix[i][right]);
            }
            for(int i=right-1;i>=left+1&&top<bottom;i--){//防止只有一行,从右往左重复添加
                record.add(matrix[bottom][i]);
            }
            for(int i=bottom;i>=top+1&&left<right;i--){//防止只有一列,从下网上重复添加
                record.add(matrix[i][left]);
            }
            left++;
            right--;
            top++;
            bottom--;
        }
        return record;
    }
}
全部评论

相关推荐

08-29 19:44
门头沟学院 Java
wish233:很强了,加上这个学历,一点小建议就是可以把你的项目做一个压测,比如说你的RPC,可以对比一下和http相比性能怎么样。还有你的秒杀,一定要自己压测一遍,确保不会出现数据不一致的问题。还有就是对于分布式锁的粒度一定要理清楚,这个很容易被问到。
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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