• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

// 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;
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
go应用docker部署(一)发布时间:2022-07-10
下一篇:
go语言web开发系列之二十一:用go-qrcode库生成二维码 - ExplorerMan发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap