//原文: // // 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; }
Cracking The Coding Interview 1.1
最新推荐文章于 2017-08-29 17:19:46 发布