题解 | #提取不重复的整数#
提取不重复的整数
https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); String s = String.valueOf(i); //对字符串末尾截取判断是否为0,如果为零重输入,不为则继续执行 String lasts = s.substring(s.length() - 1); while (lasts.equals(String.valueOf(0))){ System.out.println("请重新输入一个末尾不为零的整数"); int i1 = scanner.nextInt(); String s1 = String.valueOf(i1); String lasts1 = s1.substring(s1.length() - 1); lasts=lasts1; s=s1; } //拆分字符串 String[] split = s.split(""); //将数组放入不重复的集合中,同时从字符串末尾进行拼接 TreeSet<Object> objects = new TreeSet<>(); String a1=""; for (int j = split.length-1; j >=0; j--) { if (!objects.contains(split[j])) { objects.add(split[j]); a1=a1+split[j]; } } //输出整数 System.out.println(Integer.valueOf(a1)); } }