问题描述:squareUp问题
Given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups). squareUp(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1] squareUp(2) → [0, 1, 2, 1] squareUp(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1] |
代码:
public int[] squareUp(int n) {
int[] ans = new int[n*n];
for(int i=0;i<n*n;i++) {
int e = n-i%n;
if( e<=(i/n + 1) ) ans[i] = e;
}
return ans;
}