原题出处:https://leetcode.cn/leetbook/read/top-interview-questions-easy/xn5z8r/
解法一:
public int firstUniqChar(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.indexOf(s.charAt(i)) == s.lastIndexOf(s.charAt(i))) {
return i;
}
}
return -1;
}
思路:遍历字符串中的每一个字符,通过字符从前开始查找和通过字符向后开始查找,若找到索引值相等,则表示在该字符串中当前的字符存在不重复的值,返回当前的索引即可,若遍历的字符索引值都不相同,则表示当前字符串中每一个字符都存在相同的字符,返回-1即可。