您的位置:首页 > 其它

CareerCup1.1

2013-02-23 15:18 267 查看
Cracking the coding Interview 习题1.1:

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

代码:

#include "stdafx.h"
#include <string>
#include <iostream>
 
bool HasDuplication(std::string);
int main()
{
	using namespace std;
	string str;
	cin>>str;
	bool res = HasDuplication(str);
	if(res)
	{
		cout<<"Has Duplication"<<endl;
	}
	else
	{
		cout<<"Has All unique characters"<<endl;
	}
	return 0;
}
 
bool HasDuplication(std::string str)
{
	for(int i=0,size=str.size();i<size;i++)
	{
		for(int j=i+1;j<size;j++)
		{
			if(str[i]==str[j])
			{
				return true;
			}
		}
	}
	return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: