parent
f6be04ae40
commit
744af19f97
@ -0,0 +1,25 @@
|
||||
package hash相关.q387_字符串中的第一个唯一字符;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* Hash o(n)
|
||||
*/
|
||||
public class Solution {
|
||||
|
||||
public int firstUniqChar(String s) {
|
||||
HashMap<Character, Integer> count = new HashMap<>();
|
||||
int n = s.length();
|
||||
for (int i = 0; i < n; i++) {
|
||||
char c = s.charAt(i);
|
||||
count.put(c, count.getOrDefault(c, 0) + 1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (count.get(s.charAt(i)) == 1) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue