螺旋矩陣

題目大意

  給定一個m*n的矩陣,輸入全部元素的螺旋順序。java

解題思路

  使用計算輸出的方法,先處理上面一行,再處理右邊一列,再處理下面一行,再處理左邊一列,一直這樣操做,直到全部的元素都處理完。算法

代碼實現

算法實現類shell

import java.util.ArrayList;
import java.util.List;

public class Solution {

    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>(50);

        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return result;
        }

        // 只有一行的狀況
        if (matrix.length == 1) {
            for (int i : matrix[0]) {
                result.add(i);
            }

            return result;
        }

        // 只有一列的狀況
        if (matrix[0].length == 1) {
            for (int i = 0; i < matrix.length; i++) {
                result.add(matrix[i][0]);
            }

            return result;
        }

        // 計算有多少圈
        int row = matrix.length;
        int col = matrix[0].length;
        int cycle = row < col ? row : col;
        cycle = (cycle + 1) / 2;

        int round = 0; // 記錄當前是第幾圈
        int left = 0;
        int right = matrix[0].length - 1;
        int top = 0;
        int down = matrix.length - 1;
        int total = col*row;
        int count = 0;
        while (round < cycle) {

            // 上面一行
            for (int i = left; i <= right && count < total; i++) {
                count++;
                result.add(matrix[round][i]);
            }
            top++; //

            // 右邊一列
            for (int i = top; i <= down && count < total; i++) {
                count++;
                result.add(matrix[i][col - round - 1]);
            }
            right--;

            // 底下一行
            for (int i = right; i >= left && count < total; i--) {
                count++;
                result.add(matrix[row - round - 1][i]);

            }
            down--;

            // 左邊一列
            for (int i = down; i >= top && count < total; i--) {
                count++;
                result.add(matrix[i][round]);
            }
            left++;

            round++;
        }

        return result;
    }
}

 

螺旋矩陣是指一個呈螺旋狀的矩陣,它的數字由第一行開始到右邊不斷變大,向下變大,
  向左變大,向上變大,如此循環。ide

1this

2spa

3.net

4code

5ci

6get

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

import java.util.Scanner;

 

public class mysnakematrix {

    private int n; //

    private int a[][]; // 聲明一個矩陣

    private int value = 1;// 矩陣裏數字的值

 

    public mysnakematrix(int i) {

        this.n = i;

        a = new int[n][n];

    }

 

    // 計算第m層左上角的數字

    private int getcorner(int m) {

        int corner = 1;

        int o = n - 1;

        for (int i = 0; i < m - 1; ++i) {

            corner += 4 * o;

            o = o - 2;

        }

        return corner;

    }

 

    // 生成矩陣的每一層的每一邊的數

    // s表示4個方向,分別取值1,2,3,4,表示4個不一樣的方向。

    // o表示這條邊的起始值。

    // x表示第m層每條邊的數字個數

    private void side(int s, int o, int x, int m) {

        int i = 0;

        int j = 0;

        switch (s) {

        case 1:

            i = m - 1;

            j = m - 1;

            for (int k = 0; k < x; ++k) {

                a[i][j + k] = value;

                ++value;

            }

 

            break;

        case 2:

            i = m - 1;

            j = m - 1 + x;

            for (int k = 0; k < x; ++k) {

                a[i + k][j] = value;

                ++value;

            }

            break;

        case 3:

            i = m - 1 + x;

            j = m - 1 + x;

            for (int k = 0; k < x; ++k) {

                a[i][j - k] = value;

                ++value;

            }

            break;

        case 4:

            i = m - 1 + x;

            j = m - 1;

            for (int k = 0; k < x; ++k) {

                a[i - k][j] = value;

                ++value;

            }

            break;

        }

    }

 

    // 生成蛇形矩陣的第m層

    private void shell(int m)// m表示第m層

    {

        int x = n - 1 - (m - 1) * 2; // x表示第m層每條邊的數字個數

        int o = getcorner(m);

        int o1 = o;

        int o2 = o1 + x;

        int o3 = o2 + x;

        int o4 = o3 + x;

        // System.out.println(o4);

 

        side(1, o, x, m);

        side(2, o, x, m);

        side(3, o, x, m);

        side(4, o, x, m);

    }

 

    // 生成蛇形矩陣

    public void snakeMatrix() {

        int m = (n + 1) / 2;// 計算一共有多少層

        for (int i = 1; i <= m; ++i) {

 

            shell(i);

        }

        if (n % 2 == 1) {

            a[n / 2][n / 2] = n * n;

        }

 

    }

 

    // 打印矩陣

    public void print() {

        for (int i = 0; i < n; ++i) {

            for (int j = 0; j < n; ++j) {

                if (a[i][j] < 10) {

                    System.out.print(a[i][j] + "  ");

                } else {

                    System.out.print(a[i][j] + " ");

                }

 

            }

            System.out.println();

        }

    }

 

    public static void main(String args[]) {

        mysnakematrix my = new mysnakematrix(new Scanner(System.in).nextInt());  //利用Scanner獲取控制檯輸入

        my.snakeMatrix();

        my.print();

    }

}

相關文章
相關標籤/搜索