在一行上输入一个整数
代表给定的整数。保证
的最后一位不为
。
在一行上输出一个整数,代表处理后的数字。
9876673
37689
在这个样例中,先将数字倒序,得到
,然后去除重复数字,得到
。
12345678
87654321
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
if(in.hasNextLine()){
char[] chars = in.nextLine().toCharArray();
StringBuilder sb = new StringBuilder();
HashSet<Character> hs = new HashSet<Character> ();
for(int i=chars.length-1; i>=0; i--){
if(!hs.contains(chars[i])){
sb.append(chars[i]);
}
hs.add(chars[i]);
}
System.out.println(sb.toString());
}
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//获得倒序字符串
String str = new StringBuilder(in.nextLine()).reverse().toString();
String tmp = "";
String tp = "";
while(!str.equals("")){
//从左往右,截取字符串中第一个数字
tmp = str.substring(0,1);
//截取剩余的字符串
str = str.substring(1,str.length());
//在剩余字符串中把截取到的第一个数字tmp给全部替换掉
str = str.replaceAll(tmp,"");
//记录tmp
tp = tp + tmp;
}
System.out.print(tp);
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
in.close();
String str = Integer.toString(num);
Set<Character> set = new LinkedHashSet<>();
for (int i = str.length() - 1; i >= 0 ; i --) {
set.add(str.charAt(i));
}
for (Character set1 : set) {
System.out.print(set1);
}
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=0; //使用了的总数
boolean[] flag = new boolean[10]; //标记该数字是否被使用
String s = in.nextLine();
for(int i=s.length()-1;i>=0;i--){
int t = s.charAt(i)-'0';
if(!flag[t]){
flag[t]=true;
n++;
System.out.print(t);
}
if(n==10) //十个数字都使用了
break;
}
}
} 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());
}
}
}