zoukankan      html  css  js  c++  java
  • 请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。

    // 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;
    }
  • 相关阅读:
    输入年月日,输出这一天是这一年的多少天
    判断体重是否标准 男标准=身高-100±3 女标准=身高-110±3
    if 条件运算符
    24小时换算成12小时&&判断正负数
    运算符(编程)
    定义变量
    基础知识
    java线程阻塞中断与LockSupport使用介绍(转)
    01背包问题--动态规划解法(2)(转载)
    01背包问题--动态规划解法
  • 原文地址:https://www.cnblogs.com/wdan2016/p/6057412.html
Copyright © 2011-2022 走看看