题解 | #提取不重复的整数#
提取不重复的整数
https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1
简单题,解题思路:
1.题目关键词“不重复”,首先想到集合Set类型。但本题集合特殊(只包含固定长度的元素),即包含0-9固定长度的元素,因此可以使用数组模拟集合Set。
2.每次循环取余数,如果第一次遇到则输出即可。
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int num = in.nextInt(); boolean[] set = new boolean[10]; //boolean类型自动初始化为false while(num!=0){ int tmp = num % 10; num = num/10; if(set[tmp]==false){ System.out.print(tmp); set[tmp] = true; } } System.out.println(""); } } }