用set去重,用list排序。

Simple Sorting

https://www.nowcoder.com/practice/139761e0b59a405786898b7f2db9423f

//C++版代码
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
using namespace std;
int main() {
    int n;
    unordered_set<int> nums;
    cin >> n;
    while (n--) {
        int num;
        cin >> num;
        nums.insert(num);
    }
    vector<int> ans(nums.begin(), nums.end());
    sort(ans.begin(), ans.end());
    for (int num: ans) {
        cout << num << " ";
    }
    return 0;
}
//Java版代码
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Set<Integer> set = new HashSet<>();
        while (n-- != 0) {
            set.add(sc.nextInt());
        }
        set.stream().sorted().forEach(num -> System.out.print(num + " "));
    }
}
#Python版代码
input()
print(*sorted(set(map(int, input().split()))))

全部评论

相关推荐

哈哈哈哈哈哈哈哈哈哈这个世界太美好了
凉风落木楚山秋:毕业出路老师不管,你盖个章他好交差就完事了,等你盖完毕业了就不关他事情了
点赞 评论 收藏
分享
06-25 09:33
厦门大学 Java
程序员饺子:现在日常估计没啥hc了,等到八月多估计就慢慢有了。双九✌🏻不用焦虑的
投递快手等公司7个岗位
点赞 评论 收藏
分享
06-26 15:33
青岛工学院 Java
积极的秋田犬要冲国企:他现在邀请我明天面试
点赞 评论 收藏
分享
07-07 11:33
江南大学 Java
已经在暑假实习了&nbsp;,没有明确说有hc,纠结实习到八月份会不会有点影响秋招毕竟感觉今年好多提前批
程序员小白条:92的话准备提前批,其他没必要,没面试机会的,而且你要准备充分,尤其八股和算法题
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务