首页 > 试题广场 >

提取不重复的整数

[编程题]提取不重复的整数
  • 热度指数:519215 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入一个 int 型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。
保证输入的整数最后一位不是 0 。

数据范围:

输入描述:

输入一个int型整数



输出描述:

按照从右向左的阅读顺序,返回一个不含重复数字的新的整数

示例1

输入

9876673

输出

37689
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);
        }
    }
}

编辑于 2024-04-20 12:08:52 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        List<Integer> list = new ArrayList<>();
        while (num > 0) {
            int temp = num % 10;
            if (!list.contains(temp)) {
                list.add(temp);
            }
            num = num / 10;
        }
        for (Integer integer : list) {
            System.out.print(integer);
        }
    }
}
编辑于 2024-04-15 18:34:35 回复(0)
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);
                }
            }
        }

编辑于 2024-03-16 23:02:18 回复(0)
 while (in.hasNextInt()) { // 注意 while 处理多个 case
            StringBuilder a = new StringBuilder(in.nextLine());
            a.reverse();
            StringBuilder sb = new StringBuilder();
            for (char c : a.toString().toCharArray()) {
                if (sb.indexOf(String.valueOf(c)) == -1) {
                    sb.append(c);
                }
            }
    
            System.out.println(sb.toString());
        }
发表于 2024-03-07 17:27:08 回复(0)
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));
            }
        }
    }
}


发表于 2024-02-28 17:38:50 回复(0)

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]);
          } 
        }
    }
}

发表于 2024-02-11 23:41:34 回复(0)
  Scanner in = new Scanner(System.in);
          // 注意 hasNext 和 hasNextLine 的区别
            String line=in.nextLine();
            StringBuffer sb =new StringBuffer(line);
             sb = sb.reverse();
            String result = sb.toString();
            char[] chars = result.toCharArray();
            HashSet  set = new HashSet();
            StringBuffer restlt=new StringBuffer();
            for (int i=0;i<chars.length;i++){
                if(!set.contains(chars[i])){
                    restlt.append(chars[i]);
                    set.add(chars[i]);
                }
            }
            System.out.println(Integer.parseInt(restlt.toString()));
发表于 2023-12-21 12:03:02 回复(0)
import java.util.Scanner;
import java.util.HashSet;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            HashSet<Integer> set = new HashSet<>();
            String target = sc.nextLine();
            //Integer targetInt = Integer.valueOf(target);
            for (int i = target.length() - 1; i >= 0; i--) {
                if (set.add((int) target.charAt(i))) {
                    System.out.print(target.charAt(i));
                }
            }
        }
    }
}

发表于 2023-12-06 22:59:14 回复(0)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.TreeSet;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<String> list=new ArrayList<String>();
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            String s=String.valueOf(a);
            for(int i=s.length();i>0;i--){
                String tmp=s.substring(i-1,i);
                list.add(tmp);
            }
        List<String> myList = list.stream().distinct().collect(Collectors.toList());
        for (String i : myList) {
                System.out.print(i);
        }
    }
}
}
发表于 2023-11-30 14:49:24 回复(0)
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);
        }
    }
}

发表于 2023-11-15 19:47:36 回复(0)
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String value = String.valueOf(in.nextInt());

        String[] valueSplit = value.split("");
        Set<String> num = new LinkedHashSet<>();

        for (int i = valueSplit.length - 1; i >= 0; i--) {
            num.add(valueSplit[i]);
        }
        num.stream().forEach(System.out::print);

    }
发表于 2023-10-12 15:23:06 回复(0)
//取模从右到左获取数字,通过数组来记录已经出现的数字

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int num = scanner.nextInt();
            int result = 0;
            boolean[] cache = new boolean[10];
            boolean flag = true;
            while (flag) {
                int temp;
                if (num > 9) {
                    temp = num % 10;
                    num /= 10;
                } else {
                    temp = num;
                    flag = false;
                }
                if (cache[temp]) {
                    continue;
                }
                cache[temp] = true;
                result = result * 10 + temp;
            }
            System.out.println(result);
        }

    }
}
发表于 2023-08-11 17:43:00 回复(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);
        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);
        }
    }
}

发表于 2023-08-08 21:47:15 回复(0)
练习一下
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));
    }
}


发表于 2023-07-09 21:26:09 回复(0)
题目保证输入的整数最后一位不是 0,可以直接用数组来去重!
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);
                }
            }
        }
    }
}


发表于 2023-06-05 16:04:42 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num =in.nextInt();
        if(num<1||num>Math.pow(10,8)){
            return;
        }
        String str =String.valueOf(num);
        String newStr="";
        for(int i=str.length()-1;i>=0;i--){
            if(newStr.indexOf(str.charAt(i))==-1){
                newStr=newStr+str.charAt(i);
            }
        }
        System.out.println(Integer.parseInt(newStr));
    }
}
发表于 2023-05-30 12:17:12 回复(0)
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));
    }
}


发表于 2023-05-28 12:00:52 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int nextInt = in.nextInt();
        StringBuilder builder = new StringBuilder(String.valueOf(nextInt));
        String reverse = builder.reverse().toString();
        Set<Character> set = new HashSet<>();
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < reverse.length(); i++) {
            char c = reverse.charAt(i);
            if (!set.contains(c)) {
                list.add(c);
                set.add(c);
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (Character integer : list) {
            stringBuilder.append(integer);
        }
        System.out.println(stringBuilder.toString());
    }
}
发表于 2023-05-14 21:45:40 回复(0)

问题信息

难度:
285条回答 90668浏览

热门推荐

通过挑战的用户

查看代码