题目描述:给你一个字符串,找到这个字符串中第一个只出现过一次的字符
思路,建立一个linkedhashmap,存储每个字母出现的次数,然后再遍历这个map,找出第一个值是1的编号
java程序:
import java.util.LinkedHashMap;
public class Solution {
public int FirstNotRepeatingChar(String str) {
LinkedHashMap<Character,Integer> map = new LinkedHashMap<Character,Integer>();
int count =0;
for(int i =0;i<str.length();i++){
if(map.containsKey(str.charAt(i))){
count = map.get(str.charAt(i))+1;
map.put(str.charAt(i),count);
}else{
map.put(str.charAt(i),1);
}
}
for(int i =0;i<str.length();i++){
if(map.get(str.charAt(i))==1)
return i;
}
return -1;
}
}