forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT54.java
More file actions
39 lines (34 loc) · 1016 Bytes
/
Copy pathT54.java
File metadata and controls
39 lines (34 loc) · 1016 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
package web; /**
* @program LeetNiu
* @description: 字符流中第一个不重复的字符
* @author: mf
* @create: 2020/01/16 14:10
*/
/**
* 请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。
* 当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
*/
public class T54 {
int count[] = new int[256];
int index = 1;
public void Insert(char ch)
{
if (count[ch] == 0) {
count[ch] = index++;
} else {
count[ch] = -1;
}
}
public char FirstAppearingOnce()
{
int temp = Integer.MAX_VALUE;
char ch = '#';
for (int i = 0; i < count.length; i++) {
if (count[i] != 0 && count[i] != -1 && count[i] < temp) {
temp = count[i];
ch = (char)i;
}
}
return ch;
}
}