Vulkan

Cracking The Coding Interview 1.1

//原文:
//
//	Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?
//   
//  使用一个数组来记录该字符是否出现过,这里假定只为小写字母。
#include <iostream>
using namespace std;
bool isUnique(const char *str)
{
	int size = strlen(str);
	bool isUnique[26] = {false};
	for (int i = 0; i < size; i++)
	{
		int k = str[i] - 'a';
		if (isUnique[k])
		{
			return false;
		}
		else
			isUnique[k]=true;
	}
	return true;

}
int main()
{
	
	char s[] = "seoklncsz";
	cout << isUnique(s) << endl;
	return 0;
}




   

posted on 2014-03-31 13:26  Vulkan  阅读(140)  评论(0编辑  收藏  举报

导航