public class Try {
public static void main(String[] args){
String s = "aabcceeff";
System.out.println(firstOnce(s));
}
public static int firstOnce(String s){
int hash[] = new int[256];
int i = 0;
while (i < s.length()){ //遍历字符串,保存每个字符出现的次数
hash[s.charAt(i) - '0'] ++;
i ++;
}
i = 0;
while (i < s.length()){
if (hash[s.charAt(i) - '0'] == 1){ //再次遍历字符串,返回出现次数为1的字符在字符串中对应的位置
return i;
}
else {
i ++;
}
}
return -1;
}
}
返回字符串中第一个只出现过一次的字符(hash)
最新推荐文章于 2022-04-06 22:22:44 发布