public arraylist<integer> printmatrixclockwisely(int [][]nums){
arraylist<integer> list = new arraylist<integer>();
int rows = nums.length;
int cols = nums[0].length;
if(nums == null || cols <= 0 || rows <= 0){
return null;
}
int start = 0;
while(cols > start*2 && rows > start*2){
printmatrixincircle(list, nums, cols, rows, start);
++start;
}
return list;
}
private void printmatrixincircle(arraylist<integer> list, int[][] nums,
int cols, int rows, int start) {
int endx = cols - 1 - start;
int endy = rows - 1 - start;
//从左到右打印一行
for (int i = start; i <= endx; ++i) {
int number = nums[start][i];
list.add(number);
}
//从上到下打印一列
if(start < endy){
for (int i = start + 1; i <= endy; ++i) {
int number = nums[i][endx];
list.add(number);
}
}
//从右向左打印一行
if(start < endx && start < endy){
for (int i = endx-1; i >= start; --i) {
int number = nums[endy][i];
list.add(number);
}
}
//从下向上打印一列
if(start < endx && start < endy - 1){
for (int i = endy-1; i >= start + 1; --i) {
int number = nums[i][start];
list.add(number);
}
}
}