lotus

贵有恒何必三更眠五更起 最无益只怕一日曝十日寒

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1. 题目

读题

newcoder

提取不重复的整数

 

考查点

 

我觉得这道题的考查点有以下几个:

  • 字符串的遍历和操作,如charAt(), indexOf(), toCharArray()等方法的使用。
  • 集合类的使用,如HashSet的特性和方法,或者StringBuilder的特性和方法。
  • 数组的使用,如boolean数组的初始化和赋值。
  • 逻辑思维和编程技巧,如如何从右向左读取数字,如何判断数字是否重复,如何输出结果等。

2. 解法

思路

使用HashSet存储不重复的数字,然后从后往前遍历字符串,如果HashSet中不包含当前字符,就将其加入HashSet并输出 

代码逻辑

 

具体实现

public class HJ009 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int num = sc.nextInt();
            System.out.println(removeDuplicatied(num));
        }
//        int num = 9876673;
//        System.out.println(removeDuplicatied(num));
    }

    public static int removeDuplicatied(int num) {
        Set<Integer> set = new HashSet<>();
        String s = num + "";
        StringBuilder sb = new StringBuilder();
        for (int i = s.length() - 1; i >= 0; i--) {
            int curr = s.charAt(i) - '0';
            if (set.contains(curr)) {
                continue;
            }
            sb.append(curr);
            set.add(curr);
        }
        return Integer.parseInt(sb.toString());
    }
}

  

 

3. 总结

posted on 2023-06-07 15:23  白露~  阅读(35)  评论(0编辑  收藏  举报