输入一个 int 型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。
保证输入的整数最后一位不是 0 。
数据范围:
import java.util.ArrayList; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String str = in.nextLine(); int length = str.length() - 1; ArrayList<Character> ArrayList = new ArrayList<>(); for (int i = length; i >= 0; i--) { if (!ArrayList.contains(str.charAt(i))) { ArrayList.add(str.charAt(i)); } } for(char ch : ArrayList){ System.out.print(ch); } } }
import java.util.*; import java.io.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String s = new StringBuilder(bf.readLine()).reverse().toString(); StringBuilder sb = new StringBuilder().append(s.charAt(0)); for (int i = 1; i < s.length(); i++) { String temp = String.valueOf(s.charAt(i)); if (!sb.toString().contains(temp)) { sb.append(temp); } } System.out.println(sb); } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); StringBuilder sb = new StringBuilder(); HashSet<Character> set = new HashSet<>(); for (int i = str.length() - 1; i >= 0; i--) { char ch = str.charAt(i); if (str.lastIndexOf(ch) == i) { sb.append(ch); set.add(ch); } } System.out.println(sb.toString()); } }
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.hasNextLine()) { // 注意 while 处理多个 case String line = in.nextLine(); if(line == null || "".equals(line)){ System.out.println(""); continue; } StringBuilder sb = new StringBuilder(); char c; for(int i=line.length()-1;i >=0;i--){ c = line.charAt(i); if(i != line.length()-1){ if(line.substring(i+1,line.length()).contains(String.valueOf(c))){ continue; } } sb.append(c); } System.out.println(sb); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String string = in.nextLine(); int[] a = new int[10]; int at; StringBuilder sb = new StringBuilder(); for (int i = string.length() - 1; i >= 0; i--) { at = string.charAt(i) - '0'; if (a[at] == 0) { a[at] = 1; sb.append(string.charAt(i)); } } System.out.print(sb); } }方法2:
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String string = in.nextLine(); HashSet<Integer> set = new HashSet<>(); StringBuilder sb = new StringBuilder(); for (int i = string.length() - 1; i >= 0; i--) { if (set.contains((int) string.charAt(i))) { continue; } set.add((int) string.charAt(i)); sb.append(string.charAt(i)); } System.out.print(sb); } }
import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); //System.out.println("请输入一个个位不为零的整数:"); int num = input.nextInt(); while (num % 10 == 0){ //System.out.println("请重新输入:"); num = input.nextInt; } LinkedHashSet<Integer> hashSet = new LinkedHashSet<Integer>(); // String result = ""; int count = 1; int a = num; while (a / 10 >= 1){//判断几位数 count++; a /= 10; } for (int i = 0; i < count; i++) { int re = (int)(num / Math.pow(10, i) % 10);//从个位依次到最高位add到LinkedHashSet里 hashSet.add(re); // result += re; // System.out.println(result); } // System.out.println(result);//不能解决重复的问题 Iterator iterator = hashSet.iterator(); while (iterator.hasNext()){ System.out.print(iterator.next()); } } }
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.close(); // 使用 Set 来记录已经出现的数字 Set<Character> seenDigits = new HashSet<>(); StringBuilder result = new StringBuilder(); // 将整数转换为字符串以便逐字符处理 String numStr = Integer.toString(n); // 从右向左遍历字符串 for (int i = numStr.length() - 1; i >= 0; i--) { char digit = numStr.charAt(i); // 如果当前数字未出现过,则添加到结果中 if (!seenDigits.contains(digit)) { seenDigits.add(digit); result.append(digit); } } // 输出最终结果 System.out.println(result.toString()); } }
public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int a = in.nextInt(); Set<Integer> set = new LinkedHashSet<>(); String b = String.valueOf(a); for (int i = b.length() - 1; i >= 0 ; i--) { set.add(Integer.parseInt(b.charAt(i) + "")); } for (int s : set) { System.out.print(s); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String a = in.nextLine(); while(a.length()>0) { System.out.print(a.charAt(a.length()-1)); String t =a.charAt(a.length()-1)+""; a=a.replace(t,""); } } }
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)); } } } }