顺时针打印printMatrix
时间:2019-05-15
本文章向大家介绍顺时针打印printMatrix,主要包括顺时针打印printMatrix使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
题目:顺时针打印矩阵
要求:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
牛客网上的代码
1 import java.util.ArrayList; 2 public class Solution { 3 public ArrayList<Integer> printMatrix(int [][] matrix) { 4 //判断数组是否为空 5 if(matrix==null || matrix.length ==0 || (matrix.length==1 && matrix[0].length ==0)) 6 return null; 7 //定义列和行,方便以后的代码书写;定义要返回的ArrayList 8 int row = matrix.length; 9 int col = matrix[0].length; 10 ArrayList<Integer> result = new ArrayList<Integer>(); 11 //开始循环 12 int left=0; 13 int right= col-1; 14 int top=0; 15 int bottom = row-1; 16 while(left<=right && top<=bottom){ 17 for(int i=left;i<=right;++i) result.add(matrix[top][i]); //从左到右循环 18 //下面这一行i=top+1,容易出错 19 for(int i=top+1;i<=bottom;++i) result.add(matrix[i][right]); //从上到下循环 20 if(top!=bottom) { 21 //下面这一行i=right-1,容易出错 22 for(int i=right-1;i>=left;--i) result.add(matrix[bottom][i]); //从右到左循环 23 } 24 if(left!=right){ 25 //下面这一行i=bottom-1,容易出错;而且是i>top,没有= 26 for(int i=bottom-1;i>top;--i) result.add(matrix[i][left]); //从下到上循环 27 } 28 left++; right--; top++; bottom--; 29 } 30 return result; 31 } 32 }
本地编译器上的代码
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 public class Solution515 { 4 public static ArrayList<Integer> printMatrix(int [][] matrix) { 5 //判断数组是否为空 6 if(matrix==null || matrix.length ==0 || (matrix.length==1 && matrix[0].length ==0)) 7 return null; 8 //定义列和行,方便以后的代码书写;定义要返回的ArrayList 9 int row = matrix.length; 10 int col = matrix[0].length; 11 ArrayList<Integer> result = new ArrayList<Integer>(); 12 //开始循环 13 int left=0; 14 int right= col-1; 15 int top=0; 16 int bottom = row-1; 17 while(left<=right && top<=bottom){ 18 for(int i=left;i<=right;++i) result.add(matrix[top][i]); //从左到右循环 19 //下面这一行i=top+1,容易出错 20 for(int i=top+1;i<=bottom;++i) result.add(matrix[i][right]); //从上到下循环 21 if(top!=bottom) { 22 //下面这一行i=right-1,容易出错 23 for(int i=right-1;i>=left;--i) result.add(matrix[bottom][i]); //从右到左循环 24 } 25 if(left!=right){ 26 //下面这一行i=bottom-1,容易出错;而且是i>top,没有= 27 for(int i=bottom-1;i>top;--i) result.add(matrix[i][left]); //从下到上循环 28 } 29 left++; right--; top++; bottom--; 30 } 31 return result; 32 } 33 public static void main(String [] args) { 34 Scanner sc = new Scanner(System.in ); 35 System.out.println("输入二维数组的行数"); 36 int row = sc.nextInt(); 37 System.out.println("输入二维数组的列数"); 38 int col = sc.nextInt(); 39 int [][] array = new int[row][col]; 40 System.out.println("输入数组各个元素"); 41 for(int i=0;i<row;i++){ 42 for(int j=0;j<col;j++) 43 array[i][j]= sc.nextInt(); 44 } 45 sc.close(); 46 ArrayList<Integer> temp = new ArrayList<Integer>(); 47 temp= printMatrix(array); 48 for(int m=0;m<temp.size();m++) 49 System.out.print(temp.get(m)+" "); 50 } 51 }
原文地址:https://www.cnblogs.com/shareidea94/p/10872079.html
- udev实现热插拔
- 《Redis设计与实现》读书笔记(九) ——Redis集合和有序集合实现原理
- 内存对齐的三条原则
- 《Redis设计与实现》读书笔记(十) ——Redis对象相关其他设计与实现
- CI一些优秀实践
- 《Redis设计与实现》读书笔记(十一) ——Redis数据库与键空间
- assert断言
- 什么是堆和栈,它们在哪儿?
- contain_of宏定义
- 《Redis设计与实现》读书笔记(十三) ——Redis数据库其他内容与总结
- 《Redis设计与实现》读书笔记(十四) ——Redis RDB文件创建、载入与自动保存原理
- 框架 | Spark中的combineByKey
- 《Redis设计与实现》读书笔记(十五) ——Redis AOF持久化原理与实现
- PHP CodeBase: 生成N个不重复的随机数
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- gremlin-both()与bothE().bothV()的区别
- C# Time.deltaTime 增量时间
- Docker--docker ps 命令与结果解析
- Junit5系列-Junit5中@DisplayName自定义名称
- Junit5系列-Junit5中Assertions断言类
- 使用libev监视文件夹下文件(夹)属性变动的方案和实现
- C++拾取——stl标准库中集合交集、并集、差集、对称差方法
- netty结合Protostuff传输对象案例,单机压测秒级接收35万个对象
- Kubernates之Deployment滚动升级和回滚
- 简记特定容器list和forward_list算法
- Games101--Assignment2
- 快速学习-如何使用sentinel
- springboot-mybatis-demo遇到的坑
- 快速学习-Sentinel 工作主流程
- 快速学习-Sentinel 流量控制