Cracking the Coding Interview(1)

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>
#include <string>
using namespace std;


int unique(const string& a)
{
    bool boola[255]={0};

    for (int i=0;i<a.length();i++)
    {
        int b = (int)a[i];
        
        if(1==boola[b]) return 1;
        else boola[b]=1;
    }
    return 0;
}
int main()
{
    string a ="abcdefghijklmn";
    cout<<unique(a);
}

 

posted on 2013-02-25 14:05  mymemory  阅读(185)  评论(0编辑  收藏  举报

导航