// test20.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<string.h>
#include<deque>
#include <forward_list>
using namespace std;
class Solution
{
public:
//Insert one char from stringstream
void Insert(char ch)
{
str = str + ch;//逐个插入字符
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
// if (str == "") return NULL;
int flag = 0;
int label = -1;
for (int i = 0;i < str.size();i++)
{
flag = 0;//flag==0,表示没有重复元素
for (int j = 0;j < str.size();j++)
{
if (i != j&&str[i] == str[j])//有重复元素,则跳出循环
{
flag = 1;
break;
}
}
if (flag == 0)//判断有无重复元素,0表示无重复元素
{
label = i;
break;
}
}
if (label == -1)return '#';
return str[label];
}
void print()
{
cout << "str:" << str << endl;
}
private:
string str;
};
int main()
{
//vector<int> vec = { 49,38,65,97,76,13,27,49};
Solution so;
so.print();
so.Insert('g');
cout << "第一个不重复的字符是:" << so.FirstAppearingOnce() << endl;
so.Insert('o');
cout << "第一个不重复的字符是:" << so.FirstAppearingOnce() << endl;
so.Insert('o');
cout << "第一个不重复的字符是:" << so.FirstAppearingOnce() << endl;
so.Insert('g');
cout << "第一个不重复的字符是:" << so.FirstAppearingOnce() << endl;
so.Insert('l');
cout << "第一个不重复的字符是:" << so.FirstAppearingOnce() << endl;
so.Insert('e');
cout << "第一个不重复的字符是:" << so.FirstAppearingOnce() << endl;
// so.print();
return 0;
}
|
请发表评论