题解 | #提取不重复的整数#

提取不重复的整数

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

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        myAns();
    }

    /**
    * 方式一:使用的时间比方式二要少一点点
    */
    private static void myAns() {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            char[] ns = in.next().toCharArray();
            List<Character> list = new ArrayList<>(ns.length);
            int num = 0;
            for(int i = ns.length-1; i >= 0; i--) {
                if(!list.contains(ns[i])){
                    list.add(ns[i]);
                    num = num*10 + Integer.valueOf( ns[i]+"");
                }
            }
            System.out.println(num);
        }
    }
  
    /**
    * 方式二
    */
    private static void myAns2() {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int ns = in.nextInt();
            List<Integer> list = new ArrayList<>();
            int num = 0;
            while(ns != 0) {
                int a = ns % 10;
                if(!list.contains(a)){
                    list.add(a);
                    num = num*10 + a;
                }
                ns = ns/10;
            }
            System.out.println(num);
        }
    }
}
全部评论

相关推荐

07-09 19:25
门头沟学院 Java
这是要把每一个投校招的都开盒吗?
26届之耻将大局逆转:裁人的时候一次性追回餐费
点赞 评论 收藏
分享
06-20 17:42
东华大学 Java
凉风落木楚山秋:要是在2015,你这简历还可以月入十万,可惜现在是2025,已经跟不上版本了
我的简历长这样
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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