646 · First Position Unique Character - LintCode
# Description
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1
.
# Example
Example 1:
Input : s = "lintcode"
Output : 0
Example 2:
Input : s = "lovelintcode"
Output : 2
# Solution
public class Solution { | |
/** | |
* @param s: a string | |
* @return: it's index | |
*/ | |
public int firstUniqChar(String s) { | |
int[] count = new int[256]; | |
for(char c : s.toCharArray()){ | |
count[c]++; | |
} | |
for (int i = 0; i < s.length(); i++) { | |
if(count[s.charAt(i)] == 1){ | |
return i; | |
} | |
} | |
return -1; | |
} | |
} |
和 [209-First Unique Character in a String](Lintcode209 - First Unique Character in a String - Algorithm - Java - Computer Science | Tiffany Iong = Sleep more zzzZ..... = 努力也許會說謊,但是不會白費) 差不多的一題