返回字符串中第一个只出现过一次的字符(hash)

来源:互联网 发布:上海市云计算产业基地 编辑:程序博客网 时间:2024/05/17 17:14
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;}}

0 0
原创粉丝点击