题解 | #提取不重复的整数# 三种简洁做法
提取不重复的整数
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();
}
}
