用于流中第一个非重复字符的基于队列的方法

例子

`aabcddbe`
`a -1 b b b b c c`

`abcdabcd`
`a a a a b c d -1`

流中第一个非重复字符的算法

1. 创建一个字符队列和一个存储每个字符频率的数组。
2. 一对一地处理流的输入。
3. 对于流中当前输入的字符，请增加频率并将其推入队列。
4. 检查队列前面的字符，如果该字符的频率为1，则打印该字符，否则删除队列中的第一个字符并重复步骤4。
5. 如果在第4步中队列变空，则打印-1并继续处理下一个字符。

代码

流中第一个非重复字符的Java代码

```import java.util.LinkedList;
import java.util.Queue;

class QueueBasedApproachForFirstNonRepeatingCharacterInAStream {
private static void firstNonRepeatingCharacter(String stream) {
// create a array to store frequency of characters
int freq[] = new int[26];
// create a queue

// traverse the stream character by character
for (int i = 0; i < stream.length(); i++) {
// current character of stream
char curr = stream.charAt(i);
// increase the frequency
freq[curr - 'a']++;
// push it to queue

while (!queue.isEmpty()) {
// check the frequency of first character in queue
char front = queue.peek();
if (freq[front - 'a'] == 1) {
// if freq is 1, print the first character
System.out.print(front + " ");
break;
} else {
// else remove the first character
queue.poll();
}
}

// if queue becomes empty, print -1
if (queue.isEmpty()) {
System.out.print("-1 ");
}
}
System.out.println();
}

public static void main(String[] args) {
// Example 1
String stream1 = "aabcddbe";
firstNonRepeatingCharacter(stream1);

// Example 2
String stream2 = "abcdabcd";
firstNonRepeatingCharacter(stream2);
}
}```
```a -1 b b b b c c
a a a a b c d -1```

流中第一个非重复字符的C ++代码

```#include<bits/stdc++.h>
using namespace std;

void firstNonRepeatingCharacter(string stream) {
// create a array to store frequency of characters
int freq[26];
for (int i = 0; i < 26; i++) {
freq[i] = 0;
}
// create a queue
queue<char> q;

// traverse the stream character by character
for (int i = 0; i < stream.length(); i++) {
// current character of stream
char c = stream[i];
// increase the frequency
freq[c - 'a']++;
// push it to queue
q.push(c);

while (!q.empty()) {
// check the frequency of first character in queue
char front = q.front();
if (freq[front - 'a'] == 1) {
// if freq is 1, print the first character
cout<<front<<" ";
break;
} else {
// else remove the first character
q.pop();
}
}

// if queue becomes empty, print -1
if (q.size() == 0) {
cout<<"-1 ";
}
}
cout<<endl;
}

int main() {
// Example 1
string stream1 = "aabcddbe";
firstNonRepeatingCharacter(stream1);

// Example 2
string stream2 = "abcdabcd";
firstNonRepeatingCharacter(stream2);

return 0;
}```
```a -1 b b b b c c
a a a a b c d -1```