题解 | #字符集合#
字符集合
https://www.nowcoder.com/practice/784efd40ed8e465a84821c8f3970b7b5
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { //处理多组输入 String str = in.nextLine(); String ret = func(str); System.out.println(ret); } } public static String func(String str ){ boolean []flag = new boolean[130]; //booleaen数组,默认值都为false //哈希思想,以空间换时间, 97+26 = 123 StringBuilder sb = new StringBuilder();//用StringBuilder 重新拼接字符串 for(int i = 0;i<str.length();i++){ char c = str.charAt(i);//遍历数组,取到字符 if(flag[c]==false){//判断字符对应的数组位置上,是否为false sb.append(c);//拼接字符 flag[c]=true;//修改数组内容 } } return sb.toString(); } }