博客
关于我
[bzoj2761][暴力]不重复数字
阅读量:90 次
发布时间:2019-02-26

本文共 919 字,大约阅读时间需要 3 分钟。

要解决这个问题,我们需要去除一组数中的重复元素,只保留第一次出现的数。以下是具体的解决方案:

方法思路

  • 读取输入数据:首先读取测试用例的数量 T。对于每个测试用例,读取一个整数 N,表示接下来有 N 个数。
  • 去重处理:使用集合来存储已经出现过的数,这样可以快速检查是否已经存在。遍历输入的数,如果数不在集合中,就将其添加到集合中,并记录下来。
  • 输出结果:将保留下来的小数按顺序拼接成字符串,输出结果。
  • 这种方法利用了集合的高效查找和插入特性,确保了在处理大数据量时的性能。

    解决代码

    def main():    import sys    input = sys.stdin.read().split()    ptr = 0    T = int(input[ptr])    ptr += 1    for _ in range(T):        N = int(input[ptr])        ptr += 1        nums = list(map(int, input[ptr:ptr+N]))        ptr += N        seen = set()        res = []        for num in nums:            if num not in seen:                seen.add(num)                res.append(str(num))        print(' '.join(res))if __name__ == "__main__":    main()

    代码解释

  • 读取输入:使用 sys.stdin.read() 读取所有输入数据,并将其拆分成一个列表,处理起来更高效。
  • 处理每个测试用例:读取 T 个测试用例,每个测试用例读取 N 和接下来的 N 个数。
  • 去重处理:使用集合 seen 来记录已经出现的数,遍历输入的数列表,检查是否在集合中,不在的话添加进去,并记录到结果列表中。
  • 输出结果:将结果列表中的数转换为字符串并用空格连接,输出结果。
  • 这种方法确保了在处理大数据量时的高效性和正确性。

    转载地址:http://cymu.baihongyu.com/

    你可能感兴趣的文章
    notepad++最详情汇总
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>