classSolution { public List<Integer> spiralOrder(int[][] matrix) { if (matrix == null || matrix.length == 0) returnnewArrayList<>(); intleft=0, right = matrix[0].length - 1, up = 0, down = matrix.length - 1; List<Integer> result = newArrayList<>(); while (left <= right && up <= down) { for (inti= left; i <= right; i++) { result.add(matrix[up][i]); } for (inti= up + 1; i <= down; i++) { result.add(matrix[i][right]); } if (up != down) { for (inti= right - 1; i >= left; i--) { result.add(matrix[down][i]); } } if (left != right) { for (inti= down - 1; i > up; i--) { result.add(matrix[i][left]); } } left += 1; right -= 1; up += 1; down -= 1; } return result; } }
if (a[mid] == target) { start = mid; // this is start right = mid - 1; // lets see if there one more on the left } elseif (target > a[mid]) { left = mid + 1; } else { right = mid - 1; } }
return start; }
publicintfindEndPosition(int[] a, int target) { intleft=0; intright= a.length - 1; intend= -1;
while (left <= right) { intmid= left + (right - left) / 2;
if (a[mid] == target) { end = mid; // this is the end left = mid + 1; // lets see if there is one more on the right } elseif (target > a[mid]) left = mid + 1; else right = mid - 1;