1 条题解

  • 0
    @ 2052-11-22 10:40:47

    【例60.1】 整数去重

    解题思路

    用布尔表seen记录某值是否出现。读到未出现值立即输出并标记,出现过则跳过。

    正确性说明

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

    易错点

    不要排序,必须保持首次出现顺序。

    复杂度

    时间O(n+A),空间O(A),A=100001。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        cin >> n;
        vector<bool> seen(100001);
        bool first = true;
        while (n--) {
            int x;
            cin >> x;
            if (!seen[x]) {
                if (!first)
                    cout << " ";
                cout << x;
                first = false;
                seen[x] = true;
            }
        }
        cout << "\n";
        return 0;
    }
    
    • 1

    信息

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