马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode"
return 0.
s = "loveleetcode",
return 2.
Note: You may assume the string contain only lowercase letters.
class Solution {
int count = 0;
public int firstUniqChar(String s) {
if(s.length() == 1) return 0;
if(s.length() == 0 || count == s.length()) return -1;
if(count > s.length()) return -1;
if(count <= s.length() -1 && (s.substring(count+1,s.length()).indexOf(s.substring(count,count+1)) != -1 || s.substring(0,count).indexOf(s.substring(count,count+1)) != -1)){
count++;
return firstUniqChar(s);
}
else return count;
}
}
class Solution {
public int firstUniqChar(String s) {
if(s.length() == 0) return -1;
String [] arr = s.split("");
int max = Integer.MIN_VALUE;
for(int i = 0; i < arr.length ; i++){
if (Integer.valueOf(arr[i].charAt(0)) > max) max = Integer.valueOf(arr[i].charAt(0));
}
int[] re = new int[max+1];
for(int i = 0; i< arr.length ; i++){
re[Integer.valueOf(arr[i].charAt(0))]++;
}
for(int i = 0; i< arr.length ; i++){
if(re[Integer.valueOf(arr[i].charAt(0))] == 1) return i;
}
return -1;
}
}
class Solution {
public int firstUniqChar(String s) {
if(s.length() == 0) return -1;
int[] arr = new int[26];
for(int i = 0; i< s.length() ; i++){
arr[s.charAt(i) - 'a']++;
}
for(int i = 0; i< s.length() ; i++){
if(arr[s.charAt(i) - 'a'] == 1) return i;
}
return -1;
}
}
class Solution {
public int firstUniqChar(String s) {
if(s.length() == 0) return -1;
char[] arr = s.toCharArray();
int[] re = new int[256];
for(char i :arr) re[i]++;
for(int i = 0; i< arr.length; i++){
if(re[arr[i]] == 1) return i;
}
return -1;
}
}
|