/* * as the array is non-decreasing, thus we are sure that the elment that * has the largest squre must be at two end. */ classSolution { publicint[] sortedSquares(int[] nums) { intleft=0; intright= nums.length - 1;
classSolution { public List<Integer> spiralOrder(int[][] matrix) {
intm= matrix.length; intn= matrix[0].length; intl=0, r = n - 1, t = 0, b = m - 1;
intcnt=0; List<Integer> ans = newArrayList<>();
// && cnt < m * n // because this matrix is not a square, while ( cnt < m*n) { for (inti= l; i <= r && cnt < m * n; i++) { ans.add(matrix[t][i]); cnt++; } t++;
for (inti= t; i <= b && cnt < m * n; i++) { ans.add(matrix[i][r]); cnt++; } r--;
for (inti= r; i >= l && cnt < m * n; i--) { ans.add(matrix[b][i]); cnt++; } b--;
for (inti= b; i >= t && cnt < m * n; i--) { ans.add(matrix[i][l]); cnt++; } l++; }