// 第一种方法
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class FirstRepeat {
public:
	char findFirstRepeat(string A, int n) {
		// write code here
		vector<char> cVec;
		bool re = false;
		char ch='a';
		for (int i = 0;i < A.size();i++)
		{
			for (int j = 0;j < cVec.size();++j)
			{
				if (A[i] == cVec[j])
				{
					re = true;
					ch = A[i];
					break;
				}
			}
			if (re == false)
			{
				cVec.push_back(A[i]);
			}
			else
			{
				break;
			}
			
		}
		return ch;
	}
};
int main()
{
	string str = "qywyer23tdd";
	FirstRepeat fr;
	cout << fr.findFirstRepeat(str,str.size())<< endl;

	return 0;
};

//第二种方法
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class FirstRepeat {
public:
	char findFirstRepeat(string A, int n) {
		// write code here
	vector <int> cVec; 
	char ch;
	for (int i = 0;i < 128;i++)//建立一个存储字符的数组;共有128个字符
	{
		cVec.push_back(0);
	}
	
	for (int i = 0;i < n;i++)
	{
		int num = A[i];
	
		cVec[num]= cVec[num]++;
		cout << "char:" << A[i]  <<"   num:"<< cVec[num] << endl;
		if (cVec[num] == 2)
		{
			ch= A[i] ;

			break;
		}

	}
	return ch;
	}
};
int main()
{

	string str = "kdbaaak";
	FirstRepeat fr;
	cout << fr.findFirstRepeat(str,str.size())<< endl;

	return 0;
};

相关文章:

  • 2021-10-15
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2021-06-09
  • 2021-08-21
  • 2021-09-13
  • 2022-01-23
相关资源
相似解决方案