题解 | #字符个数统计#
字符个数统计
http://www.nowcoder.com/practice/eb94f6a5b2ba49c6ac72d40b5ce95f50
//我的写法
import java.util.*;
public class Main {public static void main(String[] args){
Scanner in = new Scanner (System.in);
while(in.hasNextLine()){
String s = in.nextLine();
char[] c = s.toCharArray();
HashMap<Integer, Character> table = new HashMap<Integer, Character>();
int j=0;
for(int i = 0; i<c.length; i++){
if( ! table.containsValue( c[i] ) ){
table.put( j++, c[i] );
}
}//for_i
System.out.println(j);
}//while
}//main
}//Main
//HashSet 基于 HashMap 来实现的,是一个不允许有重复元素的集合。无序
//其他作者的题解:
import java.util.Scanner;
import java.util.BitSet;
public class Main {
public static void main(String[] args) {
Scanner scanner = newScanner(System.in);
String line = scanner.next();
//总共有128个字符。字需要用128位
BitSet bitSet = new BitSet(128);
for(charc : line.toCharArray()) {
//判断字符c是否已出现
if(!bitSet.get(c)) {
//未出现就设置为已出现
bitSet.set(c);
}
}
//统计有多少字符已出现过
System.out.println(bitSet.cardinality());
}
}