模板

打印图示:
image.png

  1. class Solution {
  2. public List<Integer> spiralOrder(int[][] matrix) {
  3. if (matrix == null || (matrix.length == 0 && matrix[0].length == 0)) return res;
  4. int top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1;
  5. while (left <= right && top <= bottom) {
  6. for (int column = left; column <= right; column++) {
  7. res.add(matrix[top][column]);
  8. }
  9. for (int row = top + 1; row <= bottom; row++) {
  10. res.add(matrix[row][right]);
  11. }
  12. // if判断是根据是否时正方形,如果不是需要判断,如果是就不需要判断
  13. if (left < right && top < bottom) {
  14. for (int column = right - 1; column >= left + 1; column--) {
  15. res.add(matrix[bottom][column]);
  16. }
  17. for (int row = bottom; row >= top + 1; row--) {
  18. res.add(matrix[row][left]);
  19. }
  20. }
  21. top++;
  22. bottom--;
  23. left++;
  24. right--;
  25. }
  26. return res;
  27. }
  28. }

59.矩阵模拟II

🔗

  1. public int[][] generateMatrix(int n) {
  2. int[][] array = new int[n][n];
  3. int top = 0, bottom = n - 1, left = 0, right = n - 1;
  4. int count = 1;
  5. while (left <= right && top <= bottom) {
  6. for (int column = left; column <= right; column++) {
  7. array[top][column] = count++;
  8. }
  9. for (int row = top + 1; row <= bottom; row++) {
  10. array[row][right] = count++;
  11. }
  12. for (int column = right - 1; column >= left + 1; column--) {
  13. array[bottom][column] = count++;
  14. }
  15. for (int row = bottom; row >= top + 1; row--) {
  16. array[row][left] = count++;
  17. }
  18. top++;
  19. bottom--;
  20. left++;
  21. right--;
  22. }
  23. return array;
  24. }

54.螺旋矩阵

  1. public List<Integer> spiralOrder(int[][] matrix) {
  2. List<Integer> res = new ArrayList<>();
  3. if (matrix == null || (matrix.length == 0 && matrix[0].length == 0)) return res;
  4. int top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1;
  5. while (left <= right && top <= bottom) {
  6. for (int column = left; column <= right; column++) {
  7. res.add(matrix[top][column]);
  8. }
  9. for (int row = top + 1; row <= bottom; row++) {
  10. res.add(matrix[row][right]);
  11. }
  12. if (left < right && top < bottom) {
  13. for (int column = right - 1; column >= left + 1; column--) {
  14. res.add(matrix[bottom][column]);
  15. }
  16. for (int row = bottom; row >= top + 1; row--) {
  17. res.add(matrix[row][left]);
  18. }
  19. }
  20. top++;
  21. bottom--;
  22. left++;
  23. right--;
  24. }
  25. return res;
  26. }

剑指 Offer 29. 顺时针打印矩阵

🔗

  1. public int[] spiralOrder(int[][] matrix) {
  2. if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
  3. return new int[]{};
  4. }
  5. List<Integer> list = new ArrayList<>();
  6. int rows = matrix.length, columns = matrix[0].length; // 行列的数量
  7. int top = 0, button = rows - 1, right = columns - 1, left = 0;
  8. while (left <= right && top <= button) {
  9. // 遍历上边
  10. for (int column = left; column <= right; column++) {
  11. list.add(matrix[top][column]);
  12. }
  13. // 遍历右边
  14. for (int row = top + 1; row <= button; row++) {
  15. list.add(matrix[row][right]);
  16. }
  17. if (left < right && top < button) {
  18. // 遍历下边
  19. for (int column = right - 1; column > left; column--) {
  20. list.add(matrix[button][column]);
  21. }
  22. // 遍历左边
  23. for (int row = button; row > top; row--) {
  24. list.add(matrix[row][left]);
  25. }
  26. }
  27. left++;
  28. right--;
  29. top++;
  30. button--;
  31. }
  32. int[] ints = new int[list.size()];
  33. for (int i = 0; i < list.size(); i++) {
  34. ints[i] = list.get(i);
  35. }
  36. return ints;
  37. }