leetcode 387. First Unique Character in a String
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.charAt(0)) > max) max = Integer.valueOf(arr.charAt(0));
}
int[] re = new int;
for(int i = 0; i< arr.length ; i++){
re.charAt(0))]++;
}
for(int i = 0; i< arr.length ; i++){
if(re.charAt(0))] == 1) return i;
}
return -1;
}
}
class Solution {
public int firstUniqChar(String s) {
if(s.length() == 0) return -1;
int[] arr = new int;
for(int i = 0; i< s.length() ; i++){
arr++;
}
for(int i = 0; i< s.length() ; i++){
if(arr == 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;
for(char i :arr) re++;
for(int i = 0; i< arr.length; i++){
if(re] == 1) return i;
}
return -1;
}
}
页:
[1]