题目:
题解:
public static void countChar(String string) {
int letterCount = 0;
int whitespaceCount = 0;
int digitCount = 0;
int otherCount = 0;
for (char c : string.toCharArray()) {
if (Character.isLetter(c)) {
letterCount++;
continue;
}
if (Character.isWhitespace(c)) {
whitespaceCount++;
continue;
}
if (Character.isDigit(c)) {
digitCount++;
continue;
}
otherCount++;
}
System.out.println(letterCount);
System.out.println(whitespaceCount);
System.out.println(digitCount);
System.out.println(otherCount);
}
时间复杂度:O(n)