描述
在一个长为 字符串中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).(从0开始计数)
数据范围:0 \le n \le 100000≤n≤10000,且字符串只有字母组成。
要求:空间复杂度 O(n)O(n),时间复杂度 O(n)O(n)
示例1
输入: “google” 返回值: 4
输入:“aa” 返回值:-1
import java.util.*;
public class Solution {
public int FirstNotRepeatingChar(String str) {
char[] ch = str.toCharArray();
int res = -1;
for(int i = ch.length-1; i >= 0 ;i--) {
boolean f = false;
for(int j = 0;j<i;j++) {
if (ch[i] == ch[j]) {
f = true;
break;
}
}
for(int k = i+1;k < ch.length;k++){
if(ch[k] == ch[i]) {
f = true;
break;
}
}
if (!f) {
res = i;
}
}
return res;
}
}