标签:lap rac return public one span -- img http
网址:https://www.nowcoder.com/ta/cracking-the-coding-interview?query=&asc=true&order=&page=1
1.1确定字符互异
1 class Different { 2 public: 3 bool checkDifferent(string iniString) { 4 // write code here 5 int len = iniString.size(); 6 if (len > 256) 7 return false; 8 9 int cnt[255] = {0}; 10 for(int i = 0; i < len; i++) 11 { 12 int j = iniString[i]; 13 if (cnt[j]) 14 return false; 15 else 16 cnt[j] = 1; 17 } 18 return true; 19 } 20 };
1 class Different { 2 public: 3 bool checkDifferent(string iniString) { 4 // write code here 5 int len = iniString.size(); 6 if (len > 256) 7 return false; 8 9 for(int i = 0; i < len; i++) 10 { 11 for (int j = i + 1; j < len; j++) 12 { 13 if (iniString[i] == iniString[j]) 14 return false; 15 } 16 } 17 return true; 18 } 19 };
1.2原串翻转
1 class Reverse { 2 public: 3 string reverseString(string iniString) 4 { 5 // write code here 6 char ch; 7 int len = iniString.size(); 8 for(int i = 0; i < len / 2; i++) 9 { 10 ch = iniString[i]; 11 iniString[i] = iniString[len - 1 - i]; 12 iniString[len - 1 - i] = ch; 13 } 14 return iniString; 15 } 16 };
1 class Reverse { 2 public: 3 string reverseString(string iniString) 4 { 5 // write code here 6 reverse(iniString.begin(), iniString.end()); 7 return iniString; 8 } 9 };
1.3确定两串乱序同构
1 class Same { 2 public: 3 bool checkSam(string stringA, string stringB) { 4 // write code here 5 int len1 = stringA.size(); 6 int len2 = stringB.size(); 7 if(len1 != len2) 8 return false; 9 10 int cnt[257] = {0}; 11 for(int i = 0; i < len1; i++) 12 { 13 cnt[stringA[i]]++; 14 cnt[stringB[i]]--; 15 } 16 for(int i = 0; i < 256; i++) 17 { 18 if (cnt[i]) 19 return false; 20 } 21 22 return true; 23 } 24 };
标签:lap rac return public one span -- img http
原文地址:http://www.cnblogs.com/dxy1993/p/7388003.html