【问题标题】:Vector push_back() and direct assignment using [] give different results向量 push_back() 和使用 [] 的直接赋值给出不同的结果
【发布时间】:2021-12-18 12:27:31
【问题描述】:

我正在尝试解决这个 nQueens 问题,我的代码如下所示:

 class Solution {
public:
    vector<vector<string>> ans;
    
    bool canPlace(vector<string> &board, int row, int col, int n){
        //upper left diagonal
        int rowIndex = row;
        int colIndex = col;
        while(rowIndex >= 0 and colIndex >= 0){
            if(board[rowIndex][colIndex] == 'Q'){
                return false;
            }
            rowIndex--;
            colIndex--;
        }
        
        // left side
        rowIndex = row;
        colIndex = col;
        while(colIndex >= 0){
            if(board[rowIndex][colIndex] == 'Q'){
                return false;
            }
            colIndex--;
        }
        
        // left side
        rowIndex = row;
        colIndex = col;
        while(rowIndex < n and colIndex >= 0){
            if(board[rowIndex][colIndex] == 'Q'){
                return false;
            }
            rowIndex++;
            colIndex--;
        }
        
        return true;
    }
    
    void nQueens(vector<string> &board, int col, int n){
        if(col == n){
            ans.push_back(board);
            for(int i = 0; i < n; i++){
                cout<<board[i]<<", ";
            }
            cout<<endl;
            return;
        }
        
        for(int row = 0; row < n; row++){
            if(canPlace(board, row, col, n)){
                cout<<"Changing board from: "<<board[row][col]<<endl;
                board[row][col] = 'Q';
                nQueens(board, col+1,n);
                board[row][col] = '.';
            }
        }
    }
    
    vector<vector<string>> solveNQueens(int n) {
        vector<string> board(n);
        string s(n, '.');

        for(int i = 0; i < n; i++){
            board[i] = s;
            // push_back gives weird result
        }
        
        nQueens(board, 0, n);
        return ans;
    }
};

在最后一个solveNQueens 函数中。在 for 循环中,如果我使用 board.push_back(s) 而不是 board[i] = s,leetcode 会抛出 Wrong Answer 错误,并且使用 cout 时的输出会显示奇怪的随机符号。 为什么是这样? push_back 不应该给出相同的结果吗?我很想知道为什么会这样。

这里是leetcode问题的链接:https://leetcode.com/problems/n-queens

【问题讨论】:

  • 它们是不同的操作。你能解释一下为什么你期望他们给出同样的结果吗?
  • 为什么你会期待同样的结果? [] 可让您修改现有元素,而 push_back 可追加新元素。
  • 请将您的代码示例最小化为给定的问题/问题。为了解释访问向量的问题,没有人需要大量与您的问题完全无关的代码行。

标签: c++ recursion backtracking push-back n-queens


【解决方案1】:
vector<string> board(n);

已经用n 元素填充了向量。如果你现在添加额外的! push_back 的元素,你有一个包含两倍元素的向量。前半部分已经从std::vector的构造函数中进入,后半部分稍后推入。

如果你使用默认构造函数

vector<string> board;

结合push_back,您将获得与示例代码相同的结果。

对于大型向量,预先初始化n 元素并通过operator[] 访问的解决方案可以更快,因为不需要在向量内进行重新分配和复制操作。如果速度很重要:测量!

【讨论】:

  • 感谢您的回答
【解决方案2】:

push_back 在向量末尾添加一个新元素,将其大小增加 1。
assignment 不会这样做;它复制/移动值(在您的情况下,在板 vector 的那个位置 i)。

它们不一样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    相关资源
    最近更新 更多