- 首先,把字符串转为字符数组
- 定义一个哈希数组,存储遍历过的字符,当遇到首次出现3次的字符,则返回
public class Test{
public static void main(String[] args) {
String str = "Have you ever gone shopping";
System.out.println(firstThree(str));
}
public static char firstThree(String str) {
//str变成char[]
char[] data = str.toCharArray();
int[] hash = new int[256];
for (int i = 0; i < data.length; i++) {
//hash
char c = data[i];
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
hash[c]++;
if (hash[c] == 3) {
return c;
}
}
}
return '0';
}
}