zoukankan      html  css  js  c++  java
  • 顺时针打印二维矩阵

    
    
    public ArrayList printMatrix(int[][] matrix) {
    ArrayList result = new ArrayList();
    if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
    return result;
    }
    int left = 0;
    int right = matrix[0].length - 1;
    int top = 0;
    int bottom = matrix.length - 1;
    while (true) {
    //上边界
    for (int col = left; col <= right; col++) {
    result.add(matrix[top][col]);
    }
    top++;
    if (top > bottom) break;

    //右边界
    for (int row = top; row <= bottom; row++) {
    result.add(matrix[row][right]);
    }
    right--;
    if (left > right) break;

    //下边界
    for (int col = right; col >= left; col--) {
    result.add(matrix[bottom][col]);
    }
    bottom--;
    if (top > bottom) break;

    //左边界
    for (int row = bottom; row >= top; row--) {
    result.add(matrix[row][left]);
    }
    left++;
    if (left > right) break;
    }
    return result;
    }
     
  • 相关阅读:
    The Sixth Assignment
    The fifth assigiment
    网络编程
    面向对象
    python数据类型之集合
    python数据类型之列表
    python数据类型之字符串
    python数据类型之字典
    python数据类型之元组
    常用模块
  • 原文地址:https://www.cnblogs.com/wzj4858/p/15820859.html
Copyright © 2011-2022 走看看