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.
找出第一次出现的不重复的字符
public class Solution {
public int firstUniqChar(String s) {
//97-122
int[] map = new int[123];
int i=0;
int length = s.length();
for(i=0;i<length;i++)
{
map[s.charAt(i)]++;
}
for(i=0;i<length;i++)
{
if(map[s.charAt(i)] == 1)
return i;
}
return -1;
}
}
除了两次for循环,暂时没有想到N时间复杂度的解法。