二维数组顺时针打印
来源:互联网 发布:淘宝酒仙网 编辑:程序博客网 时间:2024/05/16 12:04
今天下午看到一题关于二维数组顺时针打印,感觉第二部的方法书中解释的有点脱离带水的意思,所以自己实现了一下,基本思想如下:
其实题目就是安圈来一层一层的打印,所以每次圈的起点为二维数组对称轴上的数,并且由于每一圈的结束,二维数组的行和列的数是减二,所以结束条件就是:
行<=开始点行坐标*2,或者 列<=开始点的列坐标*2,而每一圈的打印部分,则是循环打印即可,代码如下:
package str;import java.util.Vector;public class PrintMatrixInCricle {public static Vector<Integer> PrintCricle(int [][]a,int columns,int rows){Vector<Integer> vector = new Vector<Integer>();if (a.length==0||columns<=0||rows<=0)return null;int start = 0;while (columns>start*2&&rows>start*2) {GetCricle(a,columns,rows,start,vector);start++;}return vector;}private static void GetCricle(int[][] a, int columns, int rows, int start,Vector<Integer> vector) {// TODO Auto-generated method stubint i =start;int j =start;while (i<rows-start-1) {vector.add(a[start][i]);i++;}while (j<columns-start-1) {vector.add(a[j][i]);j++;}while (i>start) {vector.add(a[j][i]);i--;}while (j>start) {vector.add(a[j][i]);j--;}}public static void main(String[] args) {// TODO Auto-generated method stubint [][] a = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};Vector<Integer> vector =PrintCricle(a, a.length, a[0].length);for (int i = 0; i < vector.size(); i++) {System.out.print(vector.get(i)+"--");}}}
0 0
- 二维数组顺时针打印
- 顺时针打印二维数组
- 顺时针螺旋打印二维数组
- 剑指offer之顺时针打印二维数组
- 顺时针打印二维数组---执行判断
- 顺时针打印二维数组(螺旋输出数组)
- 顺时针打印二维矩阵
- 一个二维数组以顺时针螺旋的方式打印出来
- 顺时针打印矩阵与二维数组作为函数参数
- 剑指offer 面试题20 顺时针打印二维数组
- 二维数组顺时针输出
- 顺时针/螺旋打印数组
- 顺时针打印数组
- 顺时针打印数组
- 顺时针打印数组
- 顺时针打印数组
- 顺时针打印数组
- 顺时针数组循环打印
- [python] scapy模块的使用
- mysql事务
- 谷歌应用市场4
- 索引的作用和为什么要创建索引
- 清理ambari安装的hadoop集群
- 二维数组顺时针打印
- JAVA设计模式(10) —<结构型>桥接模式(Bridge)
- 北航上机测试准备 程序1
- select2插件的disabled功能详解
- Java程序初始化顺序
- 如何在高并发分布式系统中生成全局唯一Id
- 奇偶数分离
- 基於jquery 的 Tab
- andorid 自动化测试初探