1、题目来源:点击打开链接
2、题目:
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.
Note: You may assume the string contain only lowercase letters.
2、题目:
A、首先遍历字符串,记录每个字符出现的次数;
B、再次遍历字符串,若字符串出现次数为1,则返回对应的值。
3、代码:
public class Solution {
public int firstUniqChar(String s) {
char []str=s.toCharArray();
int count[]=new int[26];
for(char c:str){
count[c-'a']++;
}
int i=0,flag=0;
for(char c:str){
if(count[c-'a']==1){
flag=1;
break;
}
i++;
}
if(flag==0)
return -1;
return i;
}
}