leetcode 54 螺旋数组

简介

忘不了, 这是华为面试官给我的面试题, 但是我没有在1分钟内做出来. 或许那个时候面试官本来就不想要一个人.
使用模拟的方法.
使用一个visited数组, 判断是否走到边界, 只有四个方向:

  1. j++
  2. i++
  3. j--
  4. i--
    依次循环.

code

class Solution {
public:
    vector<int> res;
    void visit(int &i, int &j, vector<vector<bool>> &visited, vector<vector<int>> &matrix, int dir){
        if(dir == 1){
            for(; j<matrix[0].size(); j++){
                if(visited[i][j] == false) {
                    res.push_back(matrix[i][j]);
                    visited[i][j] = true;
                }else{
                    j--;
                    dir++;
                    i++;
                    if(i >= matrix.size() || visited[i][j] == true){
                        return;
                    }
                    visit(i, j, visited, matrix, dir);
                    break;
                }
                if(j == matrix[0].size() - 1){
                    dir++;
                    i++;
                    if(i >= matrix.size() || visited[i][j] == true){
                        return;
                    }
                    visit(i, j, visited, matrix, dir);
                    break;
                }
            }
        }
        else if(dir == 2){
            for(; i<matrix.size(); i++){
                if(visited[i][j] == false){
                    res.push_back(matrix[i][j]);
                    visited[i][j] = true;
                }else{
                    i--;
                    dir++;
                    j--;
                    if(j < 0 || visited[i][j] == true){
                        return;
                    }
                    visit(i, j, visited, matrix, dir);
                    break;
                }
                if(i == matrix.size() - 1){
                    dir++;
                    j--;
                    if(j < 0 || visited[i][j] == true){
                        return;
                    }
                    visit(i, j, visited, matrix, dir);
                    break;
                }
            }
        }
        else if(dir == 3){
            for(;j>=0; j--){
                 if(visited[i][j] == false) {
                    res.push_back(matrix[i][j]);
                    visited[i][j] = true;
                }else{
                    j++;
                    i--;
                    dir++;
                    if(i < 0 || visited[i][j] == true){
                        return;
                    }
                    visit(i, j, visited, matrix, dir);
                    break;
                }
                if(j == 0){
                    dir++;
                    i--;
                    if(i < 0 || visited[i][j] == true){
                        return;
                    }
                    visit(i, j, visited, matrix, dir);
                    break;
                }
            }
        }
        else if(dir == 4){
            for(; i>=0; i--){
                if(visited[i][j] == false){
                    res.push_back(matrix[i][j]);
                    visited[i][j] = true;
                }else{
                    i++;
                    j++;
                    dir = 1;
                    if(visited[i][j] == true){
                        return;
                    }
                    visit(i, j, visited, matrix, dir);
                    break;
                }
                if(i == 0){
                    dir = 1;
                    j++;
                    if(visited[i][j] == true){
                        return;
                    }
                    visit(i, j, visited, matrix, dir);
                    break;
                }
            }
        }
    }
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<vector<bool>> visited(matrix.size(), vector<bool>(matrix[0].size(), false));
        int dir[4] = {1, 2, 3, 4}; // i++, j++, i--, j--
        int i, j;
        i = j = 0;
        visit(i, j, visited, matrix, 1);
        return res;
    }
};

官方的代码

简短精炼, 比我这种乱七八糟的好很多. 还用到了方向数组, 我记得方向数组, 但是忘记了他是二维的数组.

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> order = new ArrayList<Integer>();
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
            return order;
        }
        int rows = matrix.length, columns = matrix[0].length;
        boolean[][] visited = new boolean[rows][columns];
        int total = rows * columns;
        int row = 0;
        int column = 0;
        int [][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int directionIndex = 0;
        for(int i = 0; i<total; i++){
            order.add(matrix[row][column]);
            visited[row][column] = true;
            int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
            if(nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]){
                directionIndex = (directionIndex + 1) % 4;
            }
            row += directions[directionIndex][0];
            column += directions[directionIndex][1];
        }
        return order;
    }
}
上一篇:[题解]剑指 Offer 48. 最长不含重复字符的子字符串 (C++)


下一篇:二维数组