输入一个 int 型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。
保证输入的整数最后一位不是 0 。
数据范围:
import java.util.LinkedHashSet; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); char[] input = in.nextLine().toCharArray(); LinkedHashSet<Integer> set = new LinkedHashSet<>(); for(int i=input.length-1;i>=0;i--){ set.add(input[i]-'0'); } for(Integer num:set) { System.out.print(num); } } }
public static void main(String[] args) { Scanner in = new Scanner(System.in); String num = in.nextLine(); int len = num.length(); HashSet<Character> set = new HashSet<>(); for (int i = len-1;i>=0;i--){ char a = num.charAt(i); if(!set.contains(a)){ set.add(a); System.out.print(a); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = in.nextLine(); HashSet has = new HashSet(); for(int i=input.length()-1;i>=0;i--){ if(has.add(input.charAt(i))){ System.out.print(input.charAt(i)); } } } }
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String str=in.next(); char[]ch=str.toCharArray(); List<Character> list=new ArrayList<>(); for(int i=ch.length-1;i>=0;i--){ if(!list.contains(ch[i])){ list.add(ch[i]); } } char[] newch=new char[list.size()]; int index=0; for(char num:list) newch[index++]=num; for(int i=0;i<newch.length;i++){ System.out.print(newch[i]); } } } }
import java.util.ArrayList; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s = sc.nextLine(); ArrayList<Integer> list=new ArrayList<>(); for (int i = s.length()-1; i >=0 ; i--) { int num = Integer.parseInt(String.valueOf(s.charAt(i))); if (list.contains(num) ==false){ list.add(num); } } for (int i : list) { System.out.print(i); } } }
import java.util.LinkedHashSet; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String nums = in.nextLine(); LinkedHashSet<Character> distinctNums = new LinkedHashSet<>(); for (int i = nums.length() - 1; i >= 0; i--) { distinctNums.add(nums.charAt(i)); } for (Character distinctNum : distinctNums) { System.out.print(distinctNum); } } }
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 a = in.nextInt(); if ((a + "").endsWith("0")) { return; } newStr(a); } } public static void newStr(int a) { String str = a + ""; String newStr = ""; for (int i = str.length(); i > 0; i--) { if (!newStr.contains(str.substring(i - 1, i))) { //newStr+=str.substring(i-1,i);//取i-1 newStr += str.charAt(i - 1); //从零开始 } } System.out.println(Integer.parseInt(newStr)); } }
public class Test04 { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case char[] chars =in.nextLine().toCharArray(); int[] arr = new int[10]; for (int i = chars.length-1; i >=0 ; i--) { int a = Integer.parseInt(chars[i] + ""); if(arr[a]==0){ arr[a]=1; System.out.print(a); } } } } }
Scanner scanner = new Scanner(System.in); StringBuilder builder = new StringBuilder(String.valueOf(scanner.nextInt())).reverse(); Set<Character> set = new HashSet<>(); String str = builder.toString(); for (int i = 0; i < str.length(); i++) { if (!set.contains(str.charAt(i))) { builder.append(str.charAt(i)); set.add(str.charAt(i)); System.out.print(str.charAt(i)); } }