careercup top 150判断字符串中字符是否唯一(字符不重复)

Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures

选自careercup top 150 questions 1.1


#include<iostream>
#include<string>
using namespace std;

bool isUniqueChar(string str){
	bool char_set[256]={0};
	int val;
	for(int i=0;i<str.length();i++){
		val=str[i];
		if(char_set[val])
			return false;
		char_set[val]=true;
	}
	return true;
}
int main(){
	
	string s="gtyr";
	bool b=isUniqueChar(s);
	cout<<b<<endl;
	b=isUniqueChar("abhgfb");
	cout<<b<<endl;
	system("pause");

	return 0;


}

假设字符是ASCII字符,每读入一个字符,把该字符ASCII值对应的char_set[]数组元素设置为true




你可能感兴趣的:(careercup top 150判断字符串中字符是否唯一(字符不重复))