题解 | #提取不重复的整数# 三种简洁做法

提取不重复的整数

https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1

private static void usingLastIndex(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNextInt()) {
                int a = in.nextInt();
                String str = Integer.toString(a);
                for (int i = str.length() - 1; i >= 0; i--) {
                    if (str.lastIndexOf(str.charAt(i)) == i) {
                        System.out.print(str.charAt(i));
                    }
                }
            }
        }
private static void usingStringBuilder(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNextInt()) {
                int a = in.nextInt();
                StringBuilder stringBuilder = new StringBuilder(String.valueOf(a)).reverse();
                String str = stringBuilder.toString();
                for (int i = 0; i < str.length(); i++) {
                    //The indexOf() method returns the position of the
                    // first occurrence of specified character(s) in a string.
                    if (str.indexOf(str.charAt(i)) == i) {
                        System.out.print(str.charAt(i));
                    }
                }
            }
        }
public static void main(String[] args) {
            //Using modulus
            Scanner in = new Scanner(System.in);
            // 注意 hasNext 和 hasNextLine 的区别
            while (in.hasNextInt()) { // 注意 while 处理多个 case
                HashSet<Integer> set = new HashSet<>();
                int a = in.nextInt();
                while (a != 0) {
                    if (set.add(a%10))
                        System.out.print(a%10);
                    a /= 10;
                }
                System.out.println();
            }
        }
全部评论

相关推荐

06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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