题解 | #字符个数统计#
字符个数统计
https://www.nowcoder.com/practice/eb94f6a5b2ba49c6ac72d40b5ce95f50
public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); int[] alphabet = new int[128]; int count = 0; for (char c : str.toCharArray()) { alphabet[c] = 1; } for (int i : alphabet) { if(i == 1){ count++; } } System.out.println(count); } }alphabet相当于hash表,遍历输入的字符串str,每个出现的字符在alphabet中标记为1。最后统计alphabet中1的数量。