题解 | #逆时针打印矩阵#

顺时针打印矩阵

http://www.nowcoder.com/practice/9b4c81a02cd34f76be2659fa0d54342a

import java.util.ArrayList; public class Solution { public ArrayList printMatrix(int [][] matrix) { int row = matrix.length - 1; int col = matrix[0].length - 1; System.out.println(row); System.out.println(col); int i = 0; int j = 0; ArrayList list = new ArrayList<>(); while(i <= row && j <= col){

        for(int temp = i;temp <= col;temp ++){
            list.add(matrix[i][temp]);
        }
       
        if(++ i > row) break;
        for(int temp = i;temp <= row;temp ++){
            list.add(matrix[temp][col]);
        }
       
        if(-- col < j) break;
        for(int temp = col ;temp >= j;temp --){
            list.add(matrix[row][temp]);
        }
      
        if(-- row < i)break;
        for(int temp = row ;temp >= i;temp --){
            list.add(matrix[temp][j]);
        }
      
        if(++ j > col) break;

    }
    return list;
}

}

全部评论

相关推荐

09-12 11:55
已编辑
湖南工商大学 Java
那一天的Java_J...:这种一堆问题的,别去
点赞 评论 收藏
分享
牛客吹哨人:哨哥晚点统一更新到黑名单:能救一个是一个!26届毁意向毁约裁员黑名单https://www.nowcoder.com/discuss/1525833
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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