网址: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 };
1.4空格替换
1 class Replacement { 2 public: 3 string replaceSpace(string iniString, int length) { 4 // write code here 5 int cnt = 0; 6 7 for (int i = 0; i < length; i++) 8 { 9 if (iniString[i] == ' ') 10 cnt++; 11 } 12 cnt = cnt * 2 + length; 13 iniString.resize(cnt); 14 cnt--; 15 16 for (int i = length - 1; i >= 0; i--) 17 { 18 if (iniString[i] == ' ') 19 { 20 iniString[cnt--] = '0'; 21 iniString[cnt--] = '2'; 22 iniString[cnt--] = '%'; 23 } 24 else 25 iniString[cnt--] = iniString[i]; 26 } 27 28 return iniString; 29 } 30 };
1.5基本字符串压缩
1 class Zipper { 2 public: 3 string zipString(string iniString) { 4 // write code here 5 int len = iniString.size(); 6 string res; 7 int cnt = 1; 8 char ch = iniString[0]; 9 10 for (int i = 1; i < len; i++) 11 { 12 if (iniString[i] == ch) 13 { 14 cnt++; 15 } 16 else 17 { 18 res += ch; 19 res += to_string(cnt); 20 cnt = 1; 21 ch = iniString[i]; 22 } 23 } 24 res += ch; 25 res += to_string(cnt); 26 27 if (res.size() >= len) 28 return iniString; 29 else 30 return res; 31 } 32 };
1.6像素翻转
1 class Transform { 2 public: 3 vector<vector<int> > transformImage(vector<vector<int> > mat, int n) { 4 // write code here 5 int t; 6 7 for (int i = 0; i < n / 2; i++) 8 { 9 for (int j = 0; j < n; j++) 10 { 11 t = mat[i][j]; 12 mat[i][j] = mat[n - 1- i][j]; 13 mat[n - 1- i][j] = t; 14 } 15 } 16 17 for (int i = 0; i < n; i++) 18 { 19 for (int j = 0; j < i; j++) 20 { 21 t = mat[i][j]; 22 mat[i][j] = mat[j][i]; 23 mat[j][i] = t; 24 } 25 } 26 return mat; 27 } 28 };
1.7清除行列
1 class Clearer { 2 public: 3 vector<vector<int> > clearZero(vector<vector<int> > mat, int n) { 4 // write code here 5 int row = 0; 6 int column = 0; 7 for (int i = 0; i < n; i++) 8 { 9 if (mat[i][0] == 0) 10 { 11 column = 1; 12 break; 13 } 14 } 15 for (int i = 0; i < n; i++) 16 { 17 if (mat[0][i] == 0) 18 { 19 row = 1; 20 break; 21 } 22 } 23 24 for (int i = 1; i < n; i++) 25 { 26 for (int j = 1; j < n; j++) 27 { 28 if (mat[i][j] == 0) 29 { 30 mat[0][j] = 0; 31 mat[i][0] = 0; 32 } 33 } 34 } 35 36 for (int i = 1; i < n; i++) 37 { 38 for (int j = 1; j < n; j++) 39 { 40 if (mat[i][0] == 0 || mat[0][j] == 0) 41 { 42 mat[i][j] = 0; 43 } 44 } 45 } 46 47 if (column) 48 { 49 for (int i = 0; i < n; i++) 50 mat[i][0] = 0; 51 } 52 if (row) 53 { 54 for (int i = 0; i < n; i++) 55 mat[0][i] = 0; 56 } 57 58 return mat; 59 } 60 };
1 class Clearer { 2 public: 3 vector<vector<int> > clearZero(vector<vector<int> > mat, int n) { 4 // write code here 5 int row = 0; 6 for (int i = 0; i < n; i++) 7 { 8 if (mat[0][i] == 0) 9 { 10 row = 1; 11 break; 12 } 13 } 14 15 for (int i = 1; i < n; i++) 16 { 17 for (int j = 0; j < n; j++) 18 { 19 if (mat[i][j] == 0) 20 { 21 mat[0][j] = 0; 22 mat[i][0] = 0; 23 } 24 } 25 } 26 27 for (int i = n - 1; i >= 1; i--) 28 { 29 for (int j = n - 1; j >= 0; j--) 30 { 31 if (mat[i][0] == 0 || mat[0][j] == 0) 32 { 33 mat[i][j] = 0; 34 } 35 } 36 } 37 38 if (row) 39 { 40 for (int i = 0; i < n; i++) 41 mat[0][i] = 0; 42 } 43 44 return mat; 45 } 46 };
1.8翻转子串
1 class ReverseEqual { 2 public: 3 bool checkReverseEqual(string s1, string s2) { 4 // write code here 5 sort(s1.begin(), s1.end()); 6 sort(s2.begin(), s2.end()); 7 8 return s1 == s2; 9 } 10 };
1 class ReverseEqual { 2 public: 3 bool checkReverseEqual(string s1, string s2) { 4 // write code here 5 int len1 = s1.size(); 6 int len2 = s2.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[s1[i]]++; 14 cnt[s2[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 };
1 class ReverseEqual { 2 public: 3 bool checkReverseEqual(string s1, string s2) { 4 // write code here 5 int len1 = s1.size(); 6 int len2 = s2.size(); 7 if (len1 != len2) 8 return false; 9 10 string s = s1 + s1; 11 if (s.find(s2) == string::npos) 12 return false; 13 else 14 return true; 15 } 16 };
2.2链表中倒数第k个结点
1 /* 2 struct ListNode { 3 int val; 4 struct ListNode *next; 5 ListNode(int x) : 6 val(x), next(NULL) { 7 } 8 };*/ 9 class Solution { 10 public: 11 ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) { 12 int i = 1; 13 ListNode* slow = pListHead; 14 ListNode* fast = pListHead; 15 16 while (i <= k && fast) 17 { 18 i++; 19 fast = fast->next; 20 } 21 22 if (i <= k) 23 return NULL; 24 else 25 { 26 while (fast) 27 { 28 fast = fast->next; 29 slow = slow->next; 30 } 31 return slow; 32 } 33 } 34 };
2.3访问单个节点的删除
1 /* 2 struct ListNode { 3 int val; 4 struct ListNode *next; 5 ListNode(int x) : val(x), next(NULL) {} 6 };*/ 7 class Remove { 8 public: 9 bool removeNode(ListNode* pNode) { 10 // write code here 11 if (pNode == NULL || pNode->next == NULL) 12 return false; 13 14 *pNode = *(pNode->next); 15 return true; 16 } 17 };
2.4链表分割
2.5链式A+B
1 /* 2 struct ListNode { 3 int val; 4 struct ListNode *next; 5 ListNode(int x) : val(x), next(NULL) {} 6 };*/ 7 class Plus { 8 public: 9 ListNode* plusAB(ListNode* a, ListNode* b) { 10 // write code here 11 ListNode* head = new ListNode(0); 12 ListNode* p = head; 13 int jin = 0; 14 int x, y, sum; 15 while (a || b || jin) 16 { 17 x = (a == NULL) ? 0 : a->val; 18 y = (b == NULL) ? 0 : b->val; 19 sum = jin + x + y; 20 p->next = new ListNode(sum % 10); 21 jin = sum / 10; 22 a = (a == NULL) ? NULL : a->next; 23 b = (b == NULL) ? NULL : b->next; 24 p = p->next; 25 } 26 p->next = NULL; 27 return head->next; 28 } 29 };
2.7回文链表
3.3集合栈
3.5用两个栈实现队列
1 class Solution 2 { 3 public: 4 void push(int node) { 5 stack1.push(node); 6 } 7 8 int pop() { 9 int num; 10 if (stack2.empty()) 11 { 12 while (!stack1.empty()) 13 { 14 stack2.push(stack1.top()); 15 stack1.pop(); 16 } 17 } 18 19 num = stack2.top(); 20 stack2.pop(); 21 return num; 22 } 23 24 private: 25 stack<int> stack1; 26 stack<int> stack2; 27 };
3.6双栈排序
4.1二叉树平衡检查
1 /* 2 struct TreeNode { 3 int val; 4 struct TreeNode *left; 5 struct TreeNode *right; 6 TreeNode(int x) : 7 val(x), left(NULL), right(NULL) { 8 } 9 };*/ 10 11 class Balance { 12 public: 13 bool flag = 1; 14 int dfs(TreeNode* root) 15 { 16 if (!flag) 17 return 0; 18 if (root == NULL) 19 return 0; 20 21 int l = dfs(root->left); 22 int r = dfs(root->right); 23 if (abs(l - r) > 1) 24 flag = 0; 25 26 return max(l, r) + 1; 27 } 28 bool isBalance(TreeNode* root) { 29 // write code here 30 if (root == NULL) 31 return true; 32 33 dfs(root); 34 return flag; 35 } 36 };