-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathFirstNotRepeatingCharInStream.java
65 lines (60 loc) · 2.36 KB
/
FirstNotRepeatingCharInStream.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
60
61
62
63
64
65
import java.util.Arrays;
/**
* Created by Yang on 2017/4/25.
* 字符流中第一个不重复的字符
*
* 题目描述
* 请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,
* 第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
* 输出描述:
* 如果当前字符流没有存在出现一次的字符,返回#字符。
*/
public class FirstNotRepeatingCharInStream {
// occurrence[i]: A character with ASCII value i;
// occurrence[i] = -1: The character has not found;
// occurrence[i] = -2: The character has been found for mutlple times
// occurrence[i] >= 0: The character has been found only once
private int[] occurrence;
private int index;
public FirstNotRepeatingCharInStream() {
occurrence = new int[256];
Arrays.fill(occurrence, -1);
index = 0;
}
//Insert one char from stringstream
public void insert(char ch)
{
if(occurrence[ch] == -1) {
occurrence[ch] = index;
} else if(occurrence[ch] >= 0) {
occurrence[ch] = -2;
}
index++;
}
//return the first appearence once char in current stringstream
public char firstAppearingOnce()
{
char firstAppearingOnceCh = '#';
int firstAppearingOnceChIndex = Integer.MAX_VALUE;
for (int i = 0; i < occurrence.length; i++) {
if(occurrence[i] >= 0 && occurrence[i] < firstAppearingOnceChIndex) {
firstAppearingOnceCh = (char)i;
firstAppearingOnceChIndex = occurrence[i];
}
}
return firstAppearingOnceCh;
}
public static void main(String[] args) {
FirstNotRepeatingCharInStream firstNotRepeatingCharInStream = new FirstNotRepeatingCharInStream();
String s = "google";
for (int i = 0; i < s.length(); i++) {
firstNotRepeatingCharInStream.insert(s.charAt(i));
}
System.out.println(firstNotRepeatingCharInStream.firstAppearingOnce() == 'l');
s = "lgooglee";
for (int i = 0; i < s.length(); i++) {
firstNotRepeatingCharInStream.insert(s.charAt(i));
}
System.out.println(firstNotRepeatingCharInStream.firstAppearingOnce() == '#');
}
}