列表

详情


JAVA38. 字符串去重

描述

从键盘获取一串字符串,要求去除重复的字符。请使用HashSet解决这一问题

输入描述

键盘输入的任意字符串

输出描述

去重后的字符串(不要求顺序,预设代码中已经给出输出)

示例1

输入:

helloworld

输出:

rdewhlo

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Java 解法, 执行用时: 13ms, 内存消耗: 9332KB, 提交时间: 2022-08-02

import java.util.HashSet;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;


public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        String str = br.readLine();

        HashSet<Character> hs = new HashSet<>();

        //write your code here......
        for(char c : str.toCharArray()){
            hs.add(c);
        }

        for (char c:hs) {
            System.out.print(c);
        }
    }
}

Java 解法, 执行用时: 13ms, 内存消耗: 9520KB, 提交时间: 2022-06-14

import java.util.HashSet;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
    public static void main(String[] args) {
         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        try {
            String str = bufferedReader.readLine();
            HashSet<Character> hs = new HashSet<>();
            char[] chars = str.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                hs.add(chars[i]);
            }
            for (char c:hs) {
                System.out.print(c);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Java 解法, 执行用时: 25ms, 内存消耗: 10480KB, 提交时间: 2022-02-08

import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        scanner.close();
        HashSet<Character> hs = new HashSet<>();

        //write your code here......
        

        char[] ch=str.toCharArray();
        hs.add(ch[0]);
        for (int i = 0; i < ch.length-1; i++) {
            if(ch[i]!=ch[i+1]){
                hs.add(ch[i+1]);
            }
        }
        for (char c:hs) {
            System.out.print(c);
        }
    }
}

Java 解法, 执行用时: 26ms, 内存消耗: 10588KB, 提交时间: 2021-11-17

import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        scanner.close();
        HashSet<Character> hs = new HashSet<>();

        //write your code here......
        for(int i = 0; i<str.length(); i++){
            hs.add(str.charAt(i));
        }

        for (char c:hs) {
            System.out.print(c);
        }
    }
}

Java 解法, 执行用时: 27ms, 内存消耗: 10424KB, 提交时间: 2021-11-26

import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        scanner.close();
        HashSet<Character> hs = new HashSet<>();

        //write your code here......
        for(int i=0;i< str.length();i++){
            hs.add(str.charAt(i));
        }

        for (char c:hs) {
            System.out.print(c);
        }
    }
}

上一题