Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 744af19

Browse files
add q387
1 parent f6be04a commit 744af19

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Hash相关
66

77
* [q1_两数之和](/src/hash相关/q1_两数之和)
8+
* [q387_字符串中的第一个唯一字符](/src/hash相关/q387_字符串中的第一个唯一字符)
89

910
### 链表操作
1011

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package hash相关.q387_字符串中的第一个唯一字符;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
* Hash o(n)
7+
*/
8+
public class Solution {
9+
10+
public int firstUniqChar(String s) {
11+
HashMap<Character, Integer> count = new HashMap<>();
12+
int n = s.length();
13+
for (int i = 0; i < n; i++) {
14+
char c = s.charAt(i);
15+
count.put(c, count.getOrDefault(c, 0) + 1);
16+
}
17+
18+
for (int i = 0; i < n; i++) {
19+
if (count.get(s.charAt(i)) == 1) {
20+
return i;
21+
}
22+
}
23+
return -1;
24+
}
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /