Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode" return 0. s = "loveleetcode", return 2.
思路:扫一遍,用array记录char的重复次数。扫第二遍,碰到第一个char的个数是1就输出,扫完没有就是-1.
public class Solution { public int firstUniqChar(String s) { if(s.length()==0) { return -1; } int[] res=new int[128]; for(int i=0;i<s.length();i++) { if(res[s.charAt(i)]==0) { res[s.charAt(i)]=1; } else { res[s.charAt(i)]++; } } for(int i=0;i<s.length();i++) { if(res[s.charAt(i)]==1) { return i; } } return -1; } }