在一個字符串(1<=字符串長度<=10000,全部由大小寫字母組成)中找到第一個只出現一次的字符,並返回它的位置


// test20.cpp : 定義控制台應用程序的入口點。
//

#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<string.h>
#include<deque>

using namespace std;

class Solution {
public:
	int FirstNotRepeatingChar(string str) {
		if (str == "")
			return -1;
		int site = 0;
		int flag = 0;
		for (int i = 0;i < str.length();i++)
		{
			char ch = str[i];
			for (int j = 0;j < str.length();j++)
			{
				if (i != j) //i和j相同的話就是比較的同一個字符
				{
					if (ch == str[j]) break;
					if (j == str.length() - 1) flag = 1; //已經找完所有的數據
				}
			
			}
			if (flag == 1)
			{
				site = i+1;
				break;
			}
		}
		return site;
	}
};
int main()
{
	
	Solution so;
	//int count = so.FirstNotRepeatingChar("wangdanwang");
	int count = so.FirstNotRepeatingChar("");
	cout << count << endl;
	
	
	
	cout << endl;
	return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM