-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main50.java
59 lines (54 loc) · 1.46 KB
/
Main50.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package JZOffer2;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
/**
* 使用哈希表存储频数
*/
public class Main50 {
public char firstUniqChar(String s) {
Map<Character, Integer> frequency = new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
frequency.put(ch, frequency.getOrDefault(ch, 0) + 1);
}
for (int i = 0; i < s.length(); ++i) {
if (frequency.get(s.charAt(i)) == 1) {
return s.charAt(i);
}
}
return ' ';
}
}
/**
* 队列
*/
class Main50_1{
public char firstUniqChar(String s) {
Map<Character, Integer> position = new HashMap<>();
Queue<Pair> queue = new LinkedList<Pair>();
int n = s.length();
for (int i = 0; i < n; i++) {
char ch = s.charAt(i);
if(!position.containsKey(ch)){
position.put(ch, i);
queue.offer(new Pair(ch, i));
}else{
position.put(ch, -1);
while(!queue.isEmpty() && position.get(queue.peek().ch) == -1){
queue.poll();
}
}
}
return queue.isEmpty() ? ' ':queue.poll().ch;
}
class Pair{
char ch;
int pos;
Pair(char ch, int pos){
this.ch = ch;
this.pos = pos;
}
}
}