forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT50.java
More file actions
39 lines (36 loc) · 1.04 KB
/
Copy pathT50.java
File metadata and controls
39 lines (36 loc) · 1.04 KB
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
package books;
import java.util.HashMap;
import java.util.Map;
/**
* @program JavaBooks
* @description: 第一个只出现一次的字符
* @author: mf
* @create: 2019/10/03 09:39
*/
/*
在字符串中找出第一个只出现一次的字符。如输入"abaccdeff",则输出b
*/
public class T50 {
public static void main(String[] args) {
Integer first = FirstNotRepeatingChar("abaccdeff");
System.out.println(first);
}
// 哈希表
private static int FirstNotRepeatingChar(String str) {
if (str == null || str.length() == 0) return -1;
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < str.length(); i++) {
if (map.containsKey(str.charAt(i))) {
map.put(str.charAt(i), map.get(str.charAt(i)) + 1);
} else {
map.put(str.charAt(i), 1);
}
}
for (int i = 0; i < str.length(); i++) {
if (map.get(str.charAt(i)) == 1) {
return i;
}
}
return -1;
}
}