题目描述
在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
ac:100%
import java.util.ArrayList;
public class Solution {
public int FirstNotRepeatingChar(String str) {
if(str.length() == 0) {
return -1;
}
ArrayList<Character> list1 = new ArrayList<Character>();//存放只出现过一次的字符
ArrayList<Character> list2 = new ArrayList<Character>();//存放出现过一次以上的字符
char temp;
for(int i = 0; i < str.length(); i++) {
temp = str.charAt(i);
if(!list1.contains(temp) && !list2.contains(temp)) {
list1.add(temp);
} else if(list1.contains(temp) && !list2.contains(temp)) {
int count = 0;
for(char x: list1) {
if(x == temp) {
list1.remove(count);
break;
}
count++;
}
list2.add(temp);
}
}
return str.indexOf(list1.get(0));
}
}