1 条题解

  • 0
    @ 1994-2-17 11:31:31

    【例51.1】 删除元素

    解题思路

    位置x按1起始转换到数组下标x-1,删除该元素,其余保持原先顺序。

    正确性说明

    按顺序完成所述运算后,每个输入元素都被处理在相应位置或统计项中。保存的中间状态与已处理数据对应,最终输出与题目定义一致。

    易错点

    删位置不是删值等于x的元素。

    复杂度

    时间O(n),空间O(n)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        cin >> n;
        vector<long long> a(n);
        for (auto &x : a)
            cin >> x;
        int pos;
        cin >> pos;
        a.erase(a.begin() + pos - 1);
        for (size_t i = 0; i < a.size(); i++) {
            if (i)
                cout << " ";
            cout << a[i];
        }
        cout << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    341
    时间
    1000ms
    内存
    64MiB
    难度
    进阶
    标签
    递交数
    0
    已通过
    0
    上传者