题解 | #二维数组打印#

二维数组打印

http://www.nowcoder.com/practice/6fadc1dac83a443c9434f350a5803b51

import java.util.*;

public class Printer {
    public int[] arrayPrint(int[][] arr, int n) {
        // write code here
        int[] ans = new int[n * n];
        int i = 0;
        for(int sy = n - 1; sy > -1; sy--) {
            int x = 0, y = sy;
            do {
                ans[i++] = arr[x++][y++];
            } while(y < n);
        }
        for(int sx = 1; sx < n; sx++) {
            int x = sx, y = 0;
            do {
                ans[i++] = arr[x++][y++];
            } while(x < n);
        }
        return ans;
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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