c++二维数组比vector 更快

做力扣的 542. 01 矩阵 题的时候,两个代码执行的速度有差距,第一个使用vector 用时200ms;第二个使用数组用时 68ms。

代码一


class Solution {
private:
     int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};  
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) { // 超时?
    queue<pair<int, int>> q;
    pair <int, int> point;
    vector<vector<int>>neibourhood = {
        {0,-1},{0,1},{-1,0},{1,0}
    };
    int row = mat.size(), col = mat[0].size();
    vector<vector<int>> result(row, vector<int>(col));
    vector<vector<int>> passed(mat.size(), vector<int>(mat[0].size())); // ? 他们有没有 pass ?
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            if (mat[i][j] == 0)
            {
                q.emplace(i,j);
                passed[i][j] = 1;
                //result[i][j] = 0;
            }
        }
    }
     
    while (!q.empty() )
    {
         
        point = q.front();
        q.pop();
        // 找这个节点的邻居
        for (auto v : neibourhood)
        {
            int new_row = point.first + v[0];
            int new_col = point.second + v[1];
            //if (new_row >= mat.size() || new_row < 0 || new_col >= mat[0].size() || new_col < 0)  continue;
            if (new_row >= 0 && new_row < row && new_col >= 0 && new_col < col && !passed[new_row][new_col])
            {
                result[new_row][new_col] = result[point.first][point.second] + 1;
                q.emplace(new_row, new_col);
                passed[new_row][new_col] = 1;
            }
             
        }
        
         
    }
    return result;

}
};

 

代码二


class Solution {
private:
     int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};  
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) { // 超时?
    queue<pair<int, int>> q;
    pair <int, int> point;
    vector<vector<int>>neibourhood2 = {
        {0,-1},{0,1},{-1,0},{1,0}
    };
    int neibourhood[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };

    int row = mat.size(), col = mat[0].size();
    vector<vector<int>> result(row, vector<int>(col));
    vector<vector<int>> passed(mat.size(), vector<int>(mat[0].size())); // ? 他们有没有 pass ?
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            if (mat[i][j] == 0)
            {
                q.emplace(i,j);
                passed[i][j] = 1;
                //result[i][j] = 0;
            }
        }
    }
     
    while (!q.empty() )
    {
         
        point = q.front();
        q.pop();
        // 找这个节点的邻居
        for (auto v : neibourhood)
        {
            int new_row = point.first + v[0];
            int new_col = point.second + v[1];
            //if (new_row >= mat.size() || new_row < 0 || new_col >= mat[0].size() || new_col < 0)  continue;
            if (new_row >= 0 && new_row < row && new_col >= 0 && new_col < col && !passed[new_row][new_col])
            {
                result[new_row][new_col] = result[point.first][point.second] + 1;
                q.emplace(new_row, new_col);
                passed[new_row][new_col] = 1;
            }
             
        }
        
         
    }
    return result;

}
};

有人知道这是为什么…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值