题解 | 提取不重复的整数

思路:

使用set去重

使用stringbuilder收集结果

倒序遍历输入

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
            String s = in.nextLine();
            Set<Character> set=new HashSet<>();
            StringBuilder ans=new StringBuilder();
            for(int i=s.length()-1;i>=0;i--){
                char c=s.charAt(i);
                if(set.contains(c)) {
                    continue;
                }
                ans.append(c);
                set.add(c);
            }
            System.out.println(ans.toString());
    }
}

全部评论

相关推荐

06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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