[相同方法的两个相同实现,为什么一个比另一个要快得多?

问题描述 投票:2回答:1

我正在尝试解决this question。我的解决方案几乎与最快的解决方案相同。但是对于一个测试用例,我有超过时间限制,而另一个(最快的)没有。谁能为我解释为什么我的代码这么慢?这是我的代码:

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int M = grid.size();
        if(M == 0) return 0;
        int N = grid[0].size();
        if(N == 0) return 0;
        int flag = 2;
        int count = 0;
        for(int i = 0; i < M; ++i){
            for(int j = 0; j < N; ++j){
                if(grid[i][j] == '1'){
                    //breadth-first search from here
                    flag++;
                    count++;
                    queue<pair<int, int>> nodes;
                    grid[i][j] = flag;
                    nodes.push({i,j});
                    while(!nodes.empty()){
                        auto node = nodes.front();
                        nodes.pop();
                        if(node.first > 0 && grid[node.first-1][node.second] == '1'){
                            grid[node.first-1][node.second] = flag;
                            nodes.push(make_pair(node.first-1, node.second));
                        }
                        if(node.first < M-1 && grid[node.first+1][node.second] == '1'){
                            grid[node.first+1][node.second] = flag;
                            nodes.push(make_pair(node.first+1, node.second));
                        }
                        if(node.second > 0 && grid[node.first][node.second-1] == '1'){
                            grid[node.first][node.second-1] = flag;
                            nodes.push(make_pair(node.first, node.second-1));
                        }
                        if(node.second < N-1 && grid[node.first][node.second + 1] == '1'){
                            grid[node.first][node.second+1] = flag;
                            nodes.push(make_pair(node.first, node.second+1));
                        }                        
                    }
                }
            }
        }
        return count;
    }
};

这里是最快的解决方案。作者非常聪明地使用数组偏移量,我认为这是他的代码和我的代码之间的唯一区别。但是我认为它不会加快代码的速度。

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int m = grid.size(), n = m ? grid[0].size() : 0, islands = 0, offsets[] = {0, 1, 0, -1, 0};
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1') {
                    islands++;
                    grid[i][j] = '0';
                    queue<pair<int, int>> todo;
                    todo.push({i, j});
                    while (!todo.empty()) {
                        pair<int, int> p = todo.front();
                        todo.pop();
                        for (int k = 0; k < 4; k++) {
                            int r = p.first + offsets[k], c = p.second + offsets[k + 1];
                            if (r >= 0 && r < m && c >= 0 && c < n && grid[r][c] == '1') {
                                grid[r][c] = '0';
                                todo.push({r, c});
                            }
                        }
                    }
                }
            }
        }
        return islands;
    }
};
c++ arrays breadth-first-search
1个回答
1
投票

这里的问题是您正在用flag的值覆盖网格上的孤岛。当flag的值等于'1'时,您的代码将进入一个无限循环,因为您正在请求带有'1'的单元格来检测孤岛。

通过对您的代码进行此额外的更改,我已接受了该问题。

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int M = grid.size();
        if(M == 0) return 0;
        int N = grid[0].size();
        if(N == 0) return 0;
        int flag = 2;
        int count = 0;
        for(int i = 0; i < M; ++i){
            for(int j = 0; j < N; ++j){
                if(grid[i][j] == '1'){
                    //breadth-first search from here
                    flag++;
                    if (flag == '1') flag++;              /////THIS LINE HERE
                    count++;
                    queue<pair<int, int>> nodes;
                    grid[i][j] = flag;
                    nodes.push({i,j});
                    while(!nodes.empty()){
                        auto node = nodes.front();
                        nodes.pop();
                        if(node.first > 0 && grid[node.first-1][node.second] == '1'){
                            grid[node.first-1][node.second] = flag;
                            nodes.push(make_pair(node.first-1, node.second));
                        }
                        if(node.first < M-1 && grid[node.first+1][node.second] == '1'){
                            grid[node.first+1][node.second] = flag;
                            nodes.push(make_pair(node.first+1, node.second));
                        }
                        if(node.second > 0 && grid[node.first][node.second-1] == '1'){
                            grid[node.first][node.second-1] = flag;
                            nodes.push(make_pair(node.first, node.second-1));
                        }
                        if(node.second < N-1 && grid[node.first][node.second + 1] == '1'){
                            grid[node.first][node.second+1] = flag;
                            nodes.push(make_pair(node.first, node.second+1));
                        }                        
                    }
                }
            }
        }
        return count;
    }
};

注:此代码仅用于说明错误,并不意味着这是解决该错误的优雅方法。

© www.soinside.com 2019 - 2024. All rights reserved.