cracking the coding interview ch1.1
2013-12-24 12:50
337 查看
Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?
判断是否有重复字符
g++ (GCC) 4.8.2 编译
编译:
$ g++ -std=c++11 ch1.1.cc -o ch1.1
运行
$ ./ch1.1
abcdefghijkl:1
abcdefghijkl:1
abcdefakldsajfjkdfjsal021830234'][:0
abcdefakldsajfjkdfjsal021830234'][:0
判断是否有重复字符
1 /************************************************************************* 2 > File Name: ch1.1.cc 3 > Author: purely 4 > Mail: godfantasy#gmail.com 5 > Created Time: 2013年12月24日 11:13:02 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<string> 10 using namespace std; 11 bool isUniqueA(const string& s) 12 { 13 bool check[255] = {0}; 14 for(const char & c : s) 15 { 16 if(check[c] == 0) 17 { 18 check[c]++; 19 } 20 else 21 { 22 return false; 23 } 24 } 25 26 return true; 27 } 28 29 bool isUniqueB(const string& s) 30 { 31 for(int i = 0; i < s.length();i++) 32 { 33 const char & c = s[i]; 34 for(int j = i + 1;j < s.length();j++) 35 { 36 if(c == s[j]) 37 { 38 return false; 39 } 40 } 41 } 42 return true; 43 } 44 45 int main() 46 { 47 string str("abcdefghijkl"); 48 cout << str << ":" << isUniqueA(str) << endl; 49 cout << str << ":" << isUniqueB(str) << endl; 50 string str1("abcdefakldsajfjkdfjsal021830234']["); 51 cout << str1 << ":" << isUniqueA(str1) << endl; 52 cout << str1 << ":" << isUniqueB(str1) << endl; 53 return 0; 54 } 55
g++ (GCC) 4.8.2 编译
编译:
$ g++ -std=c++11 ch1.1.cc -o ch1.1
运行
$ ./ch1.1
abcdefghijkl:1
abcdefghijkl:1
abcdefakldsajfjkdfjsal021830234'][:0
abcdefakldsajfjkdfjsal021830234'][:0
相关文章推荐
- Cracking the coding interview Q1.1
- Cracking The Coding Interview 3rd -- 1.1*
- cracking the coding interview ch1.2
- Cracking the Coding Interview – ch16,17,18
- cracking the coding interview No1.1
- Cracking the coding interview Q1.1
- Cracking The Coding Interview 1.1
- Cracking the coding interview--Q20.6
- Cracking the coding interview--Q3.5
- 《Cracking the Coding Interview》——第12章:测试——题目1
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
- 《Cracking the Coding Interview》——第14章:Java——题目6
- 《Cracking the Coding Interview》——第17章:普通题——题目2
- 《Cracking the Coding Interview》——第17章:普通题——题目12
- 《Cracking the Coding Interview》——第18章:难题——题目13
- Cracking the coding interview--Q9.7
- Cracking the coding interview: 查找文中两个单词的距离
- Cracking the coding interview--Q1.6
- Cracking the coding interview--Q4.6
- Cracking the coding interview--Q1.7