Java实现
用到方法
方法1
int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。
方法2
int lastIndexOf(int ch)
返回指定字符在此字符串中最后一次出现处的索引。
若二者一致,则该字符只有一个。
public class Solution {
public int FirstNotRepeatingChar(String str) {
if (str == "" || str == null)
return -1;
int temp = -1;
for (int i = 0; i < str.length(); i++) {
if (str.indexOf(str.charAt(i)) == str.lastIndexOf(str.charAt(i))){
temp = i;
break;
}
}
return temp;
}
}
京公网安备 11010502036488号