NC38 #螺旋矩阵#

螺旋矩阵

http://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31

leetcode上能过,但是在这里就过不了,不知道为啥
段错误:您的程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多)等情况引起

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int> > &matrix) {
        int direction[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int x = 0, y = 0, m = matrix.size(), n = matrix[0].size(), index = 0, count = 1;
        vector<int> vec;
        if(m == 0 || n == 0) return vec;
        vec.push_back(matrix[0][0]);
        matrix[0][0] = INT_MAX;
        while(count < m * n)
        {
            while(x + direction[index][0] < m && x + direction[index][0] >= 0 && y + direction[index][1] < n && y + direction[index][1] >= 0 && matrix[x + direction[index][0]][y + direction[index][1]] != INT_MAX)
            {
                x += direction[index][0];
                y += direction[index][1];
                vec.push_back(matrix[x][y]);
                matrix[x][y] = INT_MAX;
                count++;
            }
            index = (index + 1) % 4;
        }
        return vec;
    }
};
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务