leetcode54

题目

给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

示例 1:

输入:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]

示例 2:

输入

[

  [1,2,3,4],

  [5,6,7,8],

  [9,10,11,12],

]

输出:

[1,2,3,4,8,12,11,10,9,5,6,7]

解题思路一:

1.首先确定下一次要往那个方向走,用matrix_flag来记录四个方向的元素有没有遍历过,如果没有遍历过,并且没有越界,则说明有往这个方向走的可能。

   如果没有可走的方向则跳出遍历。

   如果有可走的方向和上一个方向相同,则下一步就是该方向;

 如果有可走的方向,并且没有可走方向与上一个方向相同,则该方向就是下一步方向。

2.确定完方向即可把进行下一步的遍历,把元素存入res中去。

代码:

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        if(matrix.empty()) return {};
        bool have_left = 0;
        bool have_right = 0;
        bool have_up = 0;
        bool have_down = 0;
        string cur_direction = "right";
        int m = matrix.size();
        int n = matrix[0].size();
        vector<int> v(n,0);
        vector<vector<int>> matrix_flag(m,v);
        vector<int> res;
        int i=0;
        int j=0;
        while(1){
            res.push_back(matrix[i][j]);
            matrix_flag[i][j] = 1;
            if(j-1>=0 && matrix_flag[i][j-1] == 0) have_left = 1;
            if(j+1 < n && matrix_flag[i][j+1] == 0) have_right = 1;
            if(i-1 >= 0 && matrix_flag[i-1][j] == 0) have_up = 1;
            if(i+1 < m && matrix_flag[i+1][j] == 0) have_down = 1;
            if(!have_left&& !have_right && !have_up && !have_down) break;
            cur_direction = nextDirection(cur_direction, have_left,have_right,have_up,have_down);   
            if(cur_direction == "left") j--;
            if(cur_direction == "right") j++;
            if(cur_direction == "up") i--;
            if(cur_direction == "down") i++;
            have_left = 0;
            have_right = 0;
            have_up = 0;
            have_down = 0;
         }
        return res;
    }
    string nextDirection(string cur_direction,int have_left,int have_right,int have_up,int have_down){
        if(cur_direction == "left" && have_left == 1) return "left";
        if(cur_direction == "right" && have_right == 1) return "right";
        if(cur_direction == "up" && have_up == 1) return "up";
        if(cur_direction == "down" && have_down == 1) return "down";
        if(have_left == 1) return "left";
        if(have_right == 1) return "right";
        if(have_up == 1) return "up";
        if(have_down == 1) return "down";
        return "nonext";
    }
};

解题思路2:

按圈进行遍历

代码:

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        if(matrix.empty()) return {};
        int m = matrix.size();
        int n = matrix[0].size();
        int left = 0,right=n-1,up=0,down=m-1;
        vector<int> res;
        while(true){
            for(int i=left;i<=right;i++) res.push_back(matrix[up][i]);
            up++;
            if(up > down) break;
            for(int i=up;i<=down;i++) res.push_back(matrix[i][right]);
            right--;
            if(right < left) break;
            for(int i=right;i>=left;i--) res.push_back(matrix[down][i]);
            down--;
            if(up > down) break;
            for(int i=down;i>=up;i--) res.push_back(matrix[i][left]);
            left++;
            if(right < left) break;
        }
        return res;
    }
};














猜你喜欢

转载自www.cnblogs.com/yxlsblog/p/10366753.html