标签:
在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符的位置。若为空串,返回-1。位置索引从0开始
public class Solution {
public int FirstNotRepeatingChar(String str) {
if (str == null || str.length() == 0) {
return -1;
}
int[] table = new int[256];
char[]strChar = str.toCharArray();
for (int i = 0; i < table.length; i++) {
table[i] = 0;
}
for (int i = 0; i < strChar.length; i++) {
table[(int)strChar[i]]++;
}
int position = 0;
while (position < strChar.length && table[(int)strChar[position]] != 1) {
position++;
}
if (position == strChar.length) {
return -1;
}
return position;
}
}
标签:
原文地址:http://www.cnblogs.com/rosending/p/5644879.html