螺旋矩阵

题目大意

  给定一个m*n的矩阵,输入所有元素的螺旋顺序。

解题思路

  使用计算输出的方法,先处理上面一行,再处理右边一列,再处理下面一行,再处理左边一列,一直这样操作,直到所有的元素都处理完。

代码实现

算法实现类

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;
    }
}

 

螺旋矩阵是指一个呈螺旋状的矩阵,它的数字由第一行开始到右边不断变大,向下变大,
  向左变大,向上变大,如此循环。

1

2

3

4

5

6

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();

    }

}

转载于:https://my.oschina.net/u/2822116/blog/806204

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值