forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFirstUniqChar.java
More file actions
44 lines (38 loc) · 925 Bytes
/
FirstUniqChar.java
File metadata and controls
44 lines (38 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package normal;
import java.util.HashMap;
/**
* @program JavaBooks
* @description: 387.字符串中的第一个唯一字符
* @author: mf
* @create: 2019/11/06 16:59
*/
/*
题目:https://leetcode-cn.com/problems/first-unique-character-in-a-string/
类型:
难度:easy
*/
/*
s = "leetcode"
返回 0.
s = "loveleetcode",
返回 2.
*/
public class FirstUniqChar {
public static void main(String[] args) {
String s = "leetcode";
String s1 = "loveleetcode";
System.out.println(firstUniqChar(s1));
}
private static int firstUniqChar(String s) {
HashMap<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
for (int i = 0; i < s.length(); i++) {
if (map.get(s.charAt(i)) == 1) {
return i;
}
}
return -1;
}
}