在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).

package MonthSep.HWday03;

// 第一个只出现一次的字符
import java.util.HashMap;
import java.util.Map;

public class HW21 {
public int FirstNotRepeatingChar(String str){
if(str == null || str.length() == 0){
return -1;
}
Map<Character, Integer> map = new HashMap<>();
for(int i = 0; i < str.length(); i++){
if(map.containsKey(str.charAt(i))){
int number = map.get(str.charAt(i));
map.put(str.charAt(i), ++number);
}else {
map.put(str.charAt(i), 1);
}
}
int pos = -1;
for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
if(map.get(c) == 1){
return i;
}
}
return pos;
}
}