├── Coding Interview Guide ├── 1.1 getMinStack.cpp ├── 1.10 getNum.cpp ├── 1.2 twoStacksQueue.cpp ├── 1.3 reverseStack.cpp ├── 1.4 catDogQueue.cpp ├── 1.5 stackSort.cpp ├── 1.7 maxWindow.cpp ├── 2.1 printCommonList.cpp ├── 2.10 addLists.cpp ├── 2.11 getIntersectNode.cpp ├── 2.12reverseKNodes.cpp ├── 2.13 removeRepNodes.cpp ├── 2.14 removeValueNode.cpp ├── 2.2 removeLastKNode.cpp ├── 2.3 removeTheNode.cpp ├── 2.4 reverseList.cpp ├── 2.5 reversePartList.cpp ├── 2.6 josephCircle.cpp ├── 2.7 isPalindromeList.cpp ├── 2.8 listPartition.cpp ├── 2.9 copyListWithRand.cpp ├── 3.1 BinaryTreeTraverse.cpp ├── 3.18 insert.cpp ├── 3.20 relocate.cpp ├── 3.4serialize.cpp ├── 4.1.1 fibonacci.cpp ├── 4.1.2 steps.cpp ├── 4.1.3 cows.cpp ├── 4.2 minPathSum.cpp ├── 4.5 longestIncreasingSubsequence.cpp ├── 4.7 longestCommonSubsequenc.cpp ├── 4.8 longestCommonSubstring.cpp ├── 5.1 isDeformation.cpp ├── 5.2 subStrSum.cpp ├── 5.3. removeZeros.cpp ├── 5.5 strToInt.cpp ├── 5.6 replace.cpp ├── 5.7 countString.cpp ├── 5.8 isUnique.cpp ├── 5.9 getIndex.cpp ├── 7.1 swapTwoNums.cpp ├── 7.2 getMax.cpp ├── 7.3 bitAlgorithm.cpp ├── 7.4 count1.cpp ├── 8.1 spiralPrintMatrix.cpp ├── 8.10 getMaxLength.cpp ├── 8.12 maxLength.cpp ├── 8.13 getSmallSum.cpp ├── 8.14 sort.cpp ├── 8.15 modify.cpp ├── 8.16 maxSubSum.cpp ├── 8.2 rotate90.cpp ├── 8.3 zigzagPrintMatrix.cpp ├── 8.4 getMinKNumsByHeap.cpp ├── 8.5 getMinLength.cpp ├── 8.6.1 printMoreHalf.cpp ├── 8.7 isContains.cpp ├── 8.8 getLongestLen.cpp ├── 8.9.1 printUniquePair.cpp ├── 8.9.2 printUniqueTriad.cpp ├── 9.2 gcd.cpp ├── 9.3.1 zeroNum.cpp └── 9.6 printAllFolds.cpp ├── Cracking the Code Interview ├── 8.1.1 isUniqueChars.cpp ├── 8.1.2 reverse.cpp ├── 8.1.3 checkAnagram.cpp ├── 8.1.4 replaceSpaces.cpp ├── 8.1.5 compressString.cpp ├── 8.1.6 transformImage.cpp ├── 8.1.7 setZeros.cpp ├── 8.1.8 isRotation.cpp ├── 8.13. 2~6.txt ├── 8.2.1 deletedups.cpp ├── 8.2.2 kthToLast.cpp ├── 8.2.3 deleteNode.cpp ├── 8.2.4 partition.cpp ├── 8.2.5 addLists.cpp ├── 8.2.5 addLists2.cpp ├── 8.2.6 findBeginning.cpp ├── 8.2.7 isPalindrome.cpp ├── 8.3.2 stackWithMin.cpp ├── 8.3.3 setOfStacks.cpp ├── 8.3.4 moveDisks.cpp ├── 8.3.5 myQueue.cpp ├── 8.3.6 stackSort.cpp ├── 8.3.7 animalQueue.cpp ├── 8.4.1 isBalance.cpp ├── 8.4.3 createMinimalBST.cpp ├── 8.4.5 checkBST.cpp ├── 8.4.6 inOrderSucc.cpp ├── 8.4.8 containsTree.cpp ├── 8.5.1 updateBits.cpp ├── 8.5.2 doubleBinary.cpp ├── 8.5.5 bitSwapRequired.cpp ├── 8.7.3 lineIntersect.cpp ├── 8.7.4 computeWithAdd.cpp ├── 8.9.1 countSteps.cpp ├── 8.9.2 countPaths.cpp ├── 8.9.3 magicIndex.cpp ├── 8.9.4 getSubsets.cpp ├── 8.9.5 permutationCombination.cpp ├── 8.9.6 generateParens.cpp └── 8.9.8 makeChange.cpp ├── HuaWeiOJ ├── 1. LengthOfLastWord.cpp ├── 2. HeChangDui.cpp ├── 3. ImageSort.cpp ├── 4. NameBeauty.cpp ├── 5. Choose 7.cpp ├── 6. StringEncrypt.cpp ├── 7. SnakeMatrix.cpp ├── 8. StringEncrypt.cpp ├── n1 lengthOfLastWord.cpp ├── n2 countChar.cpp ├── n3 randNum.cpp └── n4 partitionString.cpp ├── JianZhi Offer ├── 03_FindInPartiallySortedMatrix.cpp ├── 04_MergeArray.cpp ├── 04_ReplaceBlank.cpp ├── 05_printListFromTailToHead.cpp ├── 06_reConstructBinaryTree.cpp ├── 07_StackToQueue.cpp ├── 08_MinNumberInRotatedArray.cpp ├── 09_Fibonacci.cpp ├── 09_JumpFloor.cpp ├── 09_jumpFloorII.cpp ├── 09_rectCover.cpp ├── 10_NumberOf1.cpp ├── 11_Power.cpp ├── 12_Print1ToMaxOfNDigits.cpp ├── 13_DeleteNodeInList.cpp ├── 14_reOrderArray.cpp ├── 15_FindKthToTail.cpp ├── 16_ReverseList.cpp ├── 17_Merge.cpp ├── 18_HasSubtree.cpp ├── 19_Mirror.cpp ├── 20_printMatrix.cpp ├── 21_MinInStack.cpp ├── 22_IsPopOrder.cpp ├── 23_PrintFromTopToBottom.cpp ├── 24_VerifySquenceOfBST.cpp ├── 25_FindPath.cpp ├── 28_StringPermutation.cpp ├── 29_MoreThanHalfNum_Solution.cpp ├── 30_GetLeastNumbers_Solution.cpp ├── 31_FindGreatestSumOfSubArray.cpp ├── 32_NumberOf1Between1AndN.cpp ├── 33_PrintMinNumber.cpp ├── 34_GetUglyNumber_Solution.cpp ├── 35_FirstNotRepeatingChar.cpp ├── 38_GetNumberOfk.cpp ├── 39_IsBalanced_Solution.cpp ├── 39_TreeDepth.cpp ├── 40_FindNumsAppearOnce.cpp ├── 41_FindNumbersWithSum.cpp ├── 42_LeftRotateString.cpp ├── 42_ReverseSentence.cpp ├── 46_Sum_Solution.cpp ├── 50_getLastCommonParent.cpp ├── 51_duplicate.cpp ├── 52_multiply.cpp ├── 53_match.cpp ├── 54_isNumeric.cpp ├── 55_firstAppearingOne.cpp ├── 58_GetNext.cpp └── 59_isSymmetrical.cpp ├── README.md └── sorts ├── BubbleSort.cpp ├── HeapSort.cpp ├── InsertionSort.cpp ├── MergeSort.cpp ├── QuickSort.cpp ├── SelectionSort.cpp └── ShellSort.cpp /Coding Interview Guide/1.1 getMinStack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/1.1 getMinStack.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/1.10 getNum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/1.10 getNum.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/1.2 twoStacksQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/1.2 twoStacksQueue.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/1.3 reverseStack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 题目:仅使用递归函数和栈操作逆序一个栈 3 | 一个栈依次压入1,2,3,4,5,那么从栈顶到栈底分别为5,4,3,2,1。将这个栈转置后,从栈顶到栈底为 4 | 1,2,3,4,5,也就是实现栈中元素的逆序,但是只能用递归函数来实现,不能使用其他数据结构。 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | /*递归函数一:实现将栈底元素返回并移除*/ 15 | int getAndRemoveLastElement(stack &stk) 16 | { 17 | assert(!stk.empty()); 18 | int ret = stk.top(); 19 | stk.pop(); 20 | if (stk.empty()) 21 | return ret; 22 | else 23 | { 24 | int last = getAndRemoveLastElement(stk); 25 | stk.push(ret); 26 | return last; 27 | }//else 28 | } 29 | 30 | /*递归函数二:逆序一个栈*/ 31 | void reverse(stack &stk) 32 | { 33 | if (stk.empty()) 34 | return; 35 | int i = getAndRemoveLastElement(stk); 36 | reverse(stk); 37 | stk.push(i); 38 | } 39 | 40 | int main() 41 | { 42 | stack stk; 43 | for (int i = 1; i <= 5; ++i) 44 | stk.push(i); 45 | 46 | while (!stk.empty()) 47 | { 48 | cout << stk.top() << "\t"; 49 | stk.pop(); 50 | }//while 51 | cout << endl; 52 | system("pause"); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Coding Interview Guide/1.4 catDogQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/1.4 catDogQueue.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/1.5 stackSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/1.5 stackSort.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/1.7 maxWindow.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/1.7 maxWindow.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/2.1 printCommonList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/2.1 printCommonList.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/2.10 addLists.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/2.10 addLists.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/2.11 getIntersectNode.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/2.11 getIntersectNode.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/2.12reverseKNodes.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/2.12reverseKNodes.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/2.13 removeRepNodes.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/2.13 removeRepNodes.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/2.14 removeValueNode.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/2.14 removeValueNode.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/2.2 removeLastKNode.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/2.2 removeLastKNode.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/2.3 removeTheNode.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/2.3 removeTheNode.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/2.4 reverseList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/2.4 reverseList.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/2.5 reversePartList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/2.5 reversePartList.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/2.6 josephCircle.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/2.6 josephCircle.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/2.7 isPalindromeList.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 判断一个链表是否为回文结构; 3 | 进阶: 4 | 如果链表长度为N,时间复杂度为O(N),额外空间复杂度达到O(1) 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | struct ListNode{ 14 | int val; 15 | ListNode *next; 16 | ListNode(int v) :val(v), next(NULL){} 17 | }; 18 | 19 | ListNode *insert(ListNode *head, int val) 20 | { 21 | if (!head) 22 | head = new ListNode(val); 23 | else{ 24 | ListNode *p = head; 25 | while (p->next) 26 | p = p->next; 27 | p->next = new ListNode(val); 28 | }//else 29 | return head; 30 | } 31 | 32 | void display(ListNode *head) 33 | { 34 | if (!head) 35 | cout << endl; 36 | else{ 37 | ListNode *p = head; 38 | while (p->next) 39 | { 40 | cout << p->val << "—>"; 41 | p = p->next; 42 | }//while 43 | cout << p->val << endl; 44 | }//else 45 | } 46 | 47 | /*采用辅助栈存储链表所有节点*/ 48 | bool isPalindrom1(ListNode *head) 49 | { 50 | if (!head) 51 | return false; 52 | stack stk; 53 | ListNode *p = head; 54 | while (p) 55 | { 56 | stk.push(p->val); 57 | p = p->next; 58 | }//while 59 | 60 | p = head; 61 | while (!stk.empty() && p) 62 | { 63 | if (stk.top() != p->val) 64 | return false; 65 | stk.pop(); 66 | p = p->next; 67 | }//while 68 | return true; 69 | } 70 | 71 | /*方法二:采用辅助栈存储链表的右半部分*/ 72 | bool isPalindrom2(ListNode *head) 73 | { 74 | if (!head) 75 | return true; 76 | stack stk; 77 | 78 | ListNode *slow = head->next, *fast = head; 79 | while (fast->next && fast->next->next) 80 | { 81 | slow = slow->next; 82 | fast = fast->next->next; 83 | }//while 84 | 85 | /*此时fast指向右半部分第一个节点*/ 86 | while (fast) 87 | { 88 | stk.push(fast->val); 89 | fast = fast->next; 90 | }//while 91 | 92 | slow = head; 93 | while (!stk.empty()) 94 | { 95 | if (stk.top() != slow->val) 96 | return false; 97 | }//while 98 | return true; 99 | } 100 | 101 | /*方法三:不借助辅助结构,对链表本身右半部分反转,然后比较*/ 102 | bool isPalindrom3(ListNode *head) 103 | { 104 | if (!head) 105 | return false; 106 | ListNode *n1 = head, *n2 = head; 107 | while (n2->next &&n2->next->next) 108 | { 109 | n1 = n1->next; 110 | n2 = n2->next; 111 | }//while 112 | n2 = n1->next; /*此时q指向右半部分第一个节点*/ 113 | /*左半部分链空*/ 114 | n1->next = NULL; 115 | ListNode *n3 = NULL; 116 | while (n2) 117 | { 118 | n3 = n2->next; 119 | n2->next = n1; 120 | n1 = n2; 121 | n2 = n3; 122 | }//while 123 | n3 = n1; 124 | n2 = head; 125 | bool ret = true; 126 | while (n1 &&n2) 127 | { 128 | if (n1->val != n2->val) 129 | { 130 | ret = false; 131 | break; 132 | }//if 133 | n1 = n1->next; 134 | n2 = n2->next; 135 | }//while 136 | 137 | n1 = n3->next; 138 | n3->next = NULL; 139 | while (n1) 140 | { 141 | n2 = n1->next; 142 | n1->next = n3; 143 | n3 = n1; 144 | n1 = n2; 145 | }//while 146 | return ret; 147 | } -------------------------------------------------------------------------------- /Coding Interview Guide/2.8 listPartition.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 题目:将单链表按某值划分成左边小、中间相等右边大的形式 3 | 4 | 给定一个单向链表的头节点head,以及一个整数pivot。实现一个调整链表的函数, 5 | 将链表调整为左部分都是小于pivot的节点,中间部分都是值等于pivot的节点, 6 | 右部分都是值大于pivot的节点。 7 | 8 | 进阶: 9 | 保持左中右三个部分内部节点的相对顺序不变; 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | 18 | struct ListNode{ 19 | int val; 20 | ListNode *next; 21 | ListNode(int v) :val(v), next(NULL){} 22 | }; 23 | ListNode *insert(ListNode *head, int val) 24 | { 25 | if (!head) 26 | head = new ListNode(val); 27 | else{ 28 | ListNode *p = head; 29 | while (p->next) 30 | p = p->next; 31 | p->next = new ListNode(val); 32 | }//else 33 | return head; 34 | } 35 | 36 | void display(ListNode *head) 37 | { 38 | if (!head) 39 | cout << endl; 40 | else{ 41 | ListNode *p = head; 42 | while (p->next) 43 | { 44 | cout << p->val << "—>"; 45 | p = p->next; 46 | }//while 47 | cout << p->val << endl; 48 | }//else 49 | } 50 | 51 | 52 | /*交换下标a、b处的节点*/ 53 | void swap(vector &nodes, int a, int b) 54 | { 55 | ListNode *tmp = nodes[a]; 56 | nodes[a] = nodes[b]; 57 | nodes[b] = tmp; 58 | } 59 | 60 | /*对节点集合根据pivot进行划分排序*/ 61 | void sortPartition(vector &nodes, int pivot) 62 | { 63 | if (nodes.empty()) 64 | return; 65 | 66 | int small = -1, big = nodes.size(), idx = 0; 67 | while (idx != big) 68 | { 69 | if (nodes[idx]->val < pivot) 70 | swap(nodes, ++small, idx++); 71 | else if (nodes[idx]->val == pivot) 72 | ++idx; 73 | else 74 | swap(nodes, --big, idx); 75 | }//while 76 | } 77 | /*普通要求:解法1,借助辅助结构*/ 78 | ListNode *listPartition1(ListNode *head, int pivot) 79 | { 80 | if (!head) 81 | return head; 82 | /*将所有节点存储到vector中*/ 83 | vector nodes; 84 | ListNode *p = head; 85 | while (p) 86 | { 87 | nodes.push_back(p); 88 | p = p->next; 89 | }//while 90 | 91 | sortPartition(nodes, pivot); 92 | 93 | /*重新链接所有节点*/ 94 | for (int i = 1; i != nodes.size();++i) 95 | { 96 | nodes[i - 1]->next = nodes[i]; 97 | }//for 98 | nodes[nodes.size() - 1]->next = NULL; 99 | 100 | return nodes[0]; 101 | } 102 | 103 | /*进阶要求:对所有节点依次划分进三个链表,small equal big*/ 104 | ListNode *listPartition2(ListNode *head, int pivot) 105 | { 106 | if (!head) 107 | return head; 108 | 109 | ListNode *sH = NULL, *sT = NULL; 110 | ListNode *eH = NULL, *eT = NULL; 111 | ListNode *bH = NULL, *bT = NULL; 112 | 113 | ListNode *p = head, *r = NULL; 114 | while (p) 115 | { 116 | /*保存下一个节点*/ 117 | r = p->next; 118 | if (p->val < pivot) 119 | { 120 | if (sH == NULL) 121 | { 122 | sH = p; 123 | sT = sH; 124 | sT->next = NULL; 125 | }//if 126 | else 127 | { 128 | sT->next = p; 129 | sT = p; 130 | sT->next = NULL; 131 | }//else 132 | }//if 133 | else if (p->val == pivot) 134 | { 135 | if (eH == NULL) 136 | { 137 | eH = p; 138 | eT = eH; 139 | eT->next = NULL; 140 | }//if 141 | else{ 142 | eT->next = p; 143 | eT = p; 144 | eT->next = NULL; 145 | }//else 146 | }//elif 147 | else{ 148 | if (bH == NULL) 149 | { 150 | bH = p; 151 | bT = bH; 152 | bT->next = NULL; 153 | }//if 154 | else{ 155 | bT->next = p; 156 | bT = p; 157 | bT->next = NULL; 158 | }//else 159 | }//else 160 | p = r; 161 | }//while 162 | 163 | /*链接三个链表*/ 164 | if (sT) 165 | { 166 | sT->next = eH; 167 | eT = eT == NULL ? sT : eT; 168 | }//if 169 | if (eT) 170 | { 171 | eT->next = bH; 172 | }//if 173 | 174 | return sH != NULL ? sH : eH != NULL ? eH : bH; 175 | } 176 | 177 | int main() 178 | { 179 | ListNode *head = NULL; 180 | head = insert(head, 9); 181 | head = insert(head, 0); 182 | head = insert(head, 4); 183 | head = insert(head, 4); 184 | head = insert(head, 5); 185 | head = insert(head, 1); 186 | 187 | display(head); 188 | head = listPartition1(head, 4); 189 | display(head); 190 | 191 | head = insert(head, 9); 192 | head = insert(head, 0); 193 | head = insert(head, 4); 194 | head = insert(head, 4); 195 | head = insert(head, 5); 196 | head = insert(head, 1); 197 | 198 | display(head); 199 | head = listPartition2(head, 4); 200 | display(head); 201 | 202 | system("pause"); 203 | return 0; 204 | } -------------------------------------------------------------------------------- /Coding Interview Guide/2.9 copyListWithRand.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/2.9 copyListWithRand.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/3.1 BinaryTreeTraverse.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/3.1 BinaryTreeTraverse.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/3.18 insert.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/3.18 insert.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/3.20 relocate.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/3.20 relocate.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/3.4serialize.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/3.4serialize.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/4.1.1 fibonacci.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/4.1.1 fibonacci.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/4.1.2 steps.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/4.1.2 steps.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/4.1.3 cows.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/4.1.3 cows.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/4.2 minPathSum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/4.2 minPathSum.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/4.5 longestIncreasingSubsequence.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/4.5 longestIncreasingSubsequence.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/4.7 longestCommonSubsequenc.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/4.7 longestCommonSubsequenc.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/4.8 longestCommonSubstring.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/4.8 longestCommonSubstring.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/5.1 isDeformation.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/5.1 isDeformation.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/5.2 subStrSum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/5.2 subStrSum.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/5.3. removeZeros.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/5.3. removeZeros.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/5.5 strToInt.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/5.5 strToInt.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/5.6 replace.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/5.6 replace.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/5.7 countString.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/5.7 countString.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/5.8 isUnique.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/5.8 isUnique.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/5.9 getIndex.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/5.9 getIndex.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/7.1 swapTwoNums.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/7.1 swapTwoNums.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/7.2 getMax.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/7.2 getMax.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/7.3 bitAlgorithm.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/7.3 bitAlgorithm.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/7.4 count1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/7.4 count1.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.1 spiralPrintMatrix.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.1 spiralPrintMatrix.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.10 getMaxLength.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.10 getMaxLength.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.12 maxLength.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.12 maxLength.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.13 getSmallSum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.13 getSmallSum.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.14 sort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.14 sort.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.15 modify.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.15 modify.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.16 maxSubSum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.16 maxSubSum.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.2 rotate90.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.2 rotate90.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.3 zigzagPrintMatrix.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.3 zigzagPrintMatrix.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.4 getMinKNumsByHeap.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.4 getMinKNumsByHeap.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.5 getMinLength.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.5 getMinLength.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.6.1 printMoreHalf.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.6.1 printMoreHalf.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.7 isContains.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.7 isContains.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.8 getLongestLen.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.8 getLongestLen.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.9.1 printUniquePair.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.9.1 printUniquePair.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/8.9.2 printUniqueTriad.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/8.9.2 printUniqueTriad.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/9.2 gcd.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/9.2 gcd.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/9.3.1 zeroNum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/9.3.1 zeroNum.cpp -------------------------------------------------------------------------------- /Coding Interview Guide/9.6 printAllFolds.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Coding Interview Guide/9.6 printAllFolds.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.1.1 isUniqueChars.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 8.1.1 实现一个算法,确定一个字符串中的所有字符是否全都不同,不允许使用额外的数据结构。 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | class Solution 12 | { 13 | public: 14 | /*利用哈希的思想,保存每个字符,ASCII字符集只有256个不同的字符,最大申请空间仅仅为 256B*/ 15 | bool isUniqueChars(const string &str) 16 | { 17 | if (str.empty()) 18 | return true; 19 | else if (str.length() > 256) 20 | return false; 21 | 22 | int MAX_SIZE = 256; 23 | vector exist(MAX_SIZE, 0); 24 | 25 | int len = str.length(); 26 | for (int i = 0; i < len; ++i) 27 | { 28 | if (exist[str[i]] == 1) 29 | return false; 30 | exist[str[i]] = 1; 31 | } 32 | return true; 33 | } 34 | }; 35 | 36 | /* 37 | * 方法(2):将字符串中的每一个字符与其余字符进行比较,时间复杂度T(n)=O(n^2),空间复杂度为S(n)=O(1) 38 | * 方法(3):若允许修改字符串内容,在O(nlogn)时间内对字符串排序,然后线性检查其中有无相邻字符完全相同;注:很多排序算法会占用额外空间! 39 | */ 40 | int main() 41 | { 42 | Solution s; 43 | cout << s.isUniqueChars("very") << endl; 44 | cout << s.isUniqueChars("good") << endl; 45 | 46 | system("pause"); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Cracking the Code Interview/8.1.2 reverse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 8.1.2 反转一个null结尾的字符串 3 | */ 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class Solution{ 10 | public: 11 | //方法一:使用指针 12 | void reverse(char *str){ 13 | if (str == NULL) 14 | return; 15 | 16 | //找出字符串末尾 17 | char *end = str; 18 | if (str) 19 | { 20 | while (*end != '\0') 21 | ++end; 22 | } 23 | //回退一个字符最后一个为null 24 | --end; 25 | 26 | /*从字符串首尾开始交换,直到两个指针相遇*/ 27 | while (str < end) 28 | { 29 | char tmp = *str; 30 | *str++ = *end; 31 | *end-- = tmp; 32 | 33 | }//while 34 | return; 35 | } 36 | 37 | //方法二:使用变量 38 | void reverse2(char *str) 39 | { 40 | if (str == NULL) 41 | return; 42 | 43 | int len = 0; 44 | while (str[len] != '\0') 45 | ++len; 46 | 47 | /*从首尾交换*/ 48 | int lhs = 0, rhs = len - 1; 49 | while (lhs < rhs) 50 | { 51 | char tmp = str[lhs]; 52 | str[lhs++] = str[rhs]; 53 | str[rhs--] = tmp; 54 | }//while 55 | return; 56 | } 57 | 58 | }; 59 | 60 | int main() 61 | { 62 | Solution s; 63 | //声明的必须是字符串变量 64 | char str[] = "I am a good student!"; 65 | 66 | //下面str2是字符串常量 67 | char *str2 = "I am a good student!"; 68 | s.reverse2(str); 69 | 70 | cout << str << endl; 71 | 72 | system("pause"); 73 | return 0; 74 | } -------------------------------------------------------------------------------- /Cracking the Code Interview/8.1.3 checkAnagram.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 8.1.3 给定两个字符串,确定其中一个字符串的字符重新排列后,能否变为另一个字符串。 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | class Solution{ 14 | public: 15 | /*首先,明确题意,(1)考虑空格,(2)区分大小写*/ 16 | 17 | //方法一:字符串排序,复杂度为O(nlogn) 18 | bool checkAnagram(string str1, string str2) 19 | { 20 | //(1)首先,长度不等的字符串非变位词 21 | if (str1.empty() && str2.empty()) 22 | return true; 23 | 24 | int len1 = str1.length(), len2 = str2.length(); 25 | if (len1 != len2) 26 | return false; 27 | 28 | //(2)给字符串排序,然后比较,不等则非变位词 29 | sort(str1.begin(), str1.end()); 30 | sort(str2.begin(), str2.end()); 31 | 32 | if (str1 != str2) 33 | return false; 34 | return true; 35 | } 36 | 37 | //方法二:判断字符数是否相同 38 | bool checkAnagram2(string str1, string str2) 39 | { 40 | //(1)首先,长度不等的字符串非变位词 41 | if (str1.empty() && str2.empty()) 42 | return true; 43 | 44 | int len1 = str1.length(), len2 = str2.length(); 45 | if (len1 != len2) 46 | return false; 47 | 48 | vector letters(256, 0); 49 | for (int i = 0; i < len1; ++i) 50 | { 51 | ++letters[str1[i]]; 52 | }//for 53 | 54 | for (int j = 0; j < len2; ++j) 55 | { 56 | if (--letters[str2[j]] < 0) 57 | return false; 58 | }//for 59 | 60 | return true; 61 | } 62 | }; 63 | 64 | int main() 65 | { 66 | Solution s; 67 | cout << s.checkAnagram2("bac", "bac") << endl; 68 | 69 | system("pause"); 70 | return 0; 71 | 72 | } -------------------------------------------------------------------------------- /Cracking the Code Interview/8.1.4 replaceSpaces.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题8.1.4 编写程序,将字符串中的空格全部替换为%20。假定字符串末尾有足够的空间存放新增字符,并且知道字符串的真实长度。 3 | */ 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class Solution{ 10 | public: 11 | void replaceSpaces(char *str, int len) 12 | { 13 | if (len <= 0) 14 | return; 15 | 16 | //求出空格的个数 17 | int count = 0; 18 | for (int i = 0; i < len; ++i) 19 | { 20 | if (str[i] == ' ') 21 | ++count; 22 | } 23 | 24 | //计算新长度 25 | int newlen = len + 2 * count; 26 | str[newlen] = '\0'; 27 | for (int i = len - 1; i >= 0; --i) 28 | { 29 | if (str[i] == ' ') 30 | { 31 | str[newlen - 1] = '0'; 32 | str[newlen - 2] = '2'; 33 | str[newlen - 3] = '%'; 34 | newlen -= 3; 35 | } 36 | else{ 37 | str[newlen - 1] = str[i]; 38 | newlen -= 1; 39 | }//else 40 | }//for 41 | return; 42 | } 43 | }; 44 | 45 | int main() 46 | { 47 | Solution s; 48 | //声明的必须是字符串变量 49 | char str[] = "I am a good student!"; 50 | 51 | //下面str2是字符串常量 52 | char *str2 = "I am a good student!"; 53 | s.replaceSpaces(str , 21); 54 | 55 | cout << str << endl; 56 | 57 | system("pause"); 58 | return 0; 59 | } -------------------------------------------------------------------------------- /Cracking the Code Interview/8.1.5 compressString.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题8.1.5 利用字符串重复出现的次数,编写一个方法,实现基本的字符串压缩功能。 3 | * 比如,aabbcccccaaa会变成a2b1c5a3,若“压缩”后的字符串没有变短,则返回原先的字符串; 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | class Solution{ 13 | public: 14 | string compressString(string str) 15 | { 16 | if (str.empty()) 17 | return str; 18 | 19 | string s; 20 | //求字符串长度 21 | int len = str.length(); 22 | //记录当前首字符 23 | char cur = str[0]; 24 | int count = 1; 25 | for (int i = 1; i < len; ++i) 26 | { 27 | if (str[i] != cur) 28 | { 29 | s += cur; 30 | s = s + intToString(count); 31 | 32 | cur = str[i]; 33 | count = 1; 34 | }//if 35 | else 36 | ++count; 37 | }//for 38 | //链接末尾字符 39 | s += cur; 40 | s = s + intToString(count); 41 | 42 | if (s.length() > len) 43 | return str; 44 | return s; 45 | } 46 | 47 | string intToString(int count) 48 | { 49 | string ret = ""; 50 | while (count) 51 | { 52 | char c = count % 10 + '0'; 53 | ret += c; 54 | count /= 10; 55 | }//while 56 | reverse(ret.begin(), ret.end()); 57 | return ret; 58 | } 59 | }; 60 | 61 | int main() 62 | { 63 | Solution s; 64 | string str = s.compressString("aabcccccaaa"); 65 | cout << str << endl; 66 | 67 | system("pause"); 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Cracking the Code Interview/8.1.6 transformImage.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 题目描述 3 | 4 | 有一副由NxN矩阵表示的图像,这里每个像素用一个int表示,请编写一个算法,在不占用额外内存空间的情况下(即不使用缓存矩阵),将图像顺时针旋转90度。 5 | 给定一个NxN的矩阵,和矩阵的阶数N,请返回旋转后的NxN矩阵,保证N小于等于500,图像元素小于等于256。 6 | 测试样例: 7 | [[1,2,3],[4,5,6],[7,8,9]],3 8 | 返回:[[7,4,1],[8,5,2],[9,6,3]] 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | using namespace std; 16 | 17 | class Transform { 18 | public: 19 | vector > transformImage(vector > mat, int n) { 20 | // write code here 21 | if (mat.empty()) 22 | return mat; 23 | 24 | for (int layer = 0; layer < n/2; ++layer) 25 | { 26 | int first = layer, last = n - layer - 1; 27 | for (int i = first; i < last; ++i) 28 | { 29 | int offset = i - first; 30 | /*存储上边*/ 31 | int top = mat[first][i]; 32 | /*左到上*/ 33 | mat[first][i] = mat[last - offset][first]; 34 | 35 | /*下到左*/ 36 | mat[last - offset][first] = mat[last][last - offset]; 37 | 38 | /*右到下*/ 39 | mat[last][last - offset] = mat[i][last]; 40 | 41 | /*上到右*/ 42 | mat[i][last] = top; 43 | }//for 44 | }//for 45 | 46 | return mat; 47 | } 48 | }; 49 | 50 | int main() 51 | { 52 | vector > mat = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; 53 | for (unsigned i = 0; i < mat.size(); ++i) 54 | { 55 | for (unsigned j = 0; j < mat[i].size(); ++j) 56 | { 57 | cout << mat[i][j] << "\t"; 58 | }//for 59 | cout << endl; 60 | } 61 | Transform t; 62 | mat = t.transformImage(mat , mat.size()); 63 | for (unsigned i = 0; i < mat.size(); ++i) 64 | { 65 | for (unsigned j = 0; j < mat[i].size(); ++j) 66 | { 67 | cout << mat[i][j] << "\t"; 68 | }//for 69 | cout << endl; 70 | } 71 | system("pause"); 72 | return 0; 73 | 74 | 75 | } -------------------------------------------------------------------------------- /Cracking the Code Interview/8.1.7 setZeros.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 8.1.7 编写一个算法,若M*N矩阵中某个元素为0,则将其所在的行和列元素值清0 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Solution{ 12 | public: 13 | void setZeros(vector> &matrix) 14 | { 15 | if (matrix.empty()) 16 | return; 17 | int r = matrix.size(), c = matrix[0].size(); 18 | //标记含有0值的行和列 19 | vector rows(r, 0), cols(c, 0); 20 | 21 | for (int i = 0; i < r; ++i) 22 | { 23 | for (int j = 0; j < c; ++j) 24 | { 25 | if (matrix[i][j] == 0) 26 | { 27 | rows[i] = 1; 28 | cols[j] = 1; 29 | }//if 30 | }//for 31 | }//for 32 | 33 | //将标记的行和列元素清0 34 | for (int i = 0; i < r; ++i) 35 | { 36 | for (int j = 0; j < c; ++j) 37 | { 38 | if (rows[i] || cols[j]) 39 | matrix[i][j] = 0; 40 | }//for 41 | }//for 42 | } 43 | }; 44 | 45 | int main() 46 | { 47 | Solution s; 48 | vector> v = { { 1, 2, 0 }, { 2, 4, 5 }, { 0, 3, 4 } }; 49 | 50 | s.setZeros(v); 51 | 52 | for (unsigned i = 0; i < v.size(); ++i) 53 | { 54 | for (unsigned j = 0; j < v[i].size(); ++j) 55 | { 56 | cout << v[i][j] << "\t"; 57 | }//for 58 | cout << endl; 59 | }//for 60 | 61 | system("pause"); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Cracking the Code Interview/8.1.8 isRotation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 8.1.8 假定有一个方法isSubstring 可检查一个单词是否为其它字符串的子串。 3 | * 给定两个字符串s1和s2,请编写代码检查s2是否由s1旋转而成,要求只能调用一次isSubstring 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | class Solution 12 | { 13 | public: 14 | bool isRotation(string s1, string s2) 15 | { 16 | //检查两个字符串是否等长且不为空 17 | if (s1.empty() || s2.empty() || s1.length() != s2.length()) 18 | return false; 19 | return isSubstring(s1 + s1, s2); 20 | } 21 | 22 | bool isSubstring(string str, string sub) 23 | { 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /Cracking the Code Interview/8.13. 2~6.txt: -------------------------------------------------------------------------------- 1 | ^(* ̄(oo) ̄)^ 2 | 关于C和C++的几个关键问题: 3 | ======================================================================================= 4 | 8.13.2 比较并对比散列表和STL map。散列表是如何实现的?如果输入的数据量不大, 5 | 可以选用哪些数据结构替代散列表? 6 | 答: 7 | 在散列表里,值的存放是通过将键传入散列函数实现的。值并不是以排序后的顺序存放。此外, 8 | 散列表以键找出索引,进而找到存放值的地方,因此,插入或查找操作均摊后可以在O(1)的时 9 | 间内完成(假定该散列表很少发生碰撞冲突)。散列表还必须处理潜在的碰撞冲突,一般通过 10 | 拉链法(chaining)解决,也即是创建一个链表来存放值,这些值的键都映射到同一个索引。 11 | 12 | STL map的做法是根据键,将键值对插入二叉查找树。不需要处理冲突,因为树是平衡的,插入 13 | 和查找操作的时间肯定为O(logN)。 14 | 15 | 散列表是如何实现的? 16 | 传统上,散列表都是用元素为链表的数组实现的。想要插入键值对时,先用散列函数将键映射 17 | 为数组索引,随后,将值插入那个索引位置对应的链表。 18 | 注意,在数组的特定索引位置的链表中,各个元素的键并不相同,这些值的hashFunction(key) 19 | 才是相同的。因此,为了取回某个键对应的值,每个结点都必须存放键和值。 20 | 总而言之,散列表会以链表数组的形式实现,链表中每个结点都会存放两块数据:值和原先的 21 | 键。此外,我们还要注意以下设计准则、 22 | (1)我们希望使用一个优良的散列函数,确保能将键均匀分散开来。若分散不均匀,就会发生 23 | 大量碰撞冲突,查找元素的速度也会变慢。 24 | (2)不论散列函数选的多好,还是会出现碰撞冲突,因此需要一种碰撞处理方法。通常,我们 25 | 会选择拉链法,也就是通过链表来处理,但这并不是唯一的做法。 26 | (3)我们可能还希望设法根据容量动态扩大或缩小散列表的大小。例如,当元素数量和散列表 27 | 大小之比超过一定阈值时,可能会希望扩大散列表的大小。这意味着要新建一个散列表,并将旧 28 | 的散列表条目转移到新的散列表中。因为这种操作的开销非常大,所以我们要谨慎些,不可频繁 29 | 操作。 30 | 31 | 如果输入的数据量不大,可以选用哪些数据结构代替散列表? 32 | 可以使用STL map或者二叉树。尽管两者的插入操作需要O(log(n))的时间,但若是输入数据量够 33 | 小,这点时间可以忽略不计。 34 | 35 | ========================================================================================== 36 | 8.13.3 C++虚函数的工作原理是什么? 37 | 答: 38 | 虚函数(virtual function)需要虚函数表(Vtable,Virtual Table)才能实现。如果一个类有 39 | 函数声明成虚拟的,就会生成一个Vtable,存放这个类的虚函数地址。此外,编译器还会在类里加 40 | 入隐藏的vptr变量。若子类没有覆写虚函数,该子类的Vtable就会存放父类的函数地址。调用这个 41 | 虚函数时,就会通过Vtable解析函数的地址。在C++里,动态绑定(dynamic binding)就是通过 42 | Vtable机制实现的。 43 | 由此,将子类对象赋值给基类指针时,vptr变量就会指向子类的Vtable。这样一来,就能确保继承 44 | 关系最末端的子类虚函数会被调用到。 45 | 46 | 举例: 47 | class Shape{ 48 | public: 49 | int edge_length; 50 | virtual int circumference(){ 51 | cout<<"Circumference of Base Class.\n"; 52 | return 0; 53 | } 54 | }; 55 | 56 | class Triangle : public Shape{ 57 | public: 58 | int circumference(){ 59 | cout<<"Circumference of Triangle Class.\n"; 60 | return 3 * edge_length; 61 | } 62 | }; 63 | 64 | void main() 65 | { 66 | Shape *x = new Shape(); 67 | x->circumference(); //"Circumference of Base Class." 68 | Shape *y = new Triangle(); 69 | y->circumference(); //"Circumference of Triangle Class." 70 | } 71 | 在上面的代码中,circumference是Shape类的虚函数,因此在所有继承Shape类的子类(Triangle 72 | 类等)里都是虚函数。在C++里,非虚函数的调用是在编译期间通过静态绑定实现的,而虚函数的 73 | 调用则是在运行期间通过动态绑定实现的。 74 | 75 | ========================================================================================== 76 | 8.13.4 深拷贝和浅拷贝之间有何区别?请说明两者用法。 77 | 答: 78 | 浅拷贝会将对象所有成员的值拷贝到另一个对象里。除了拷贝所有成员的值,深拷贝还会进一步拷 79 | 贝所有指针对象。 80 | 81 | 举例: 82 | struct Test{ 83 | char *ptr; 84 | }; 85 | 86 | void shallow_copy(Test &src , Test &dest) 87 | { 88 | dest.ptr = src.ptr; 89 | } 90 | 91 | void deep_copy(Test &src , Test &dest) 92 | { 93 | dest.ptr = (char *)malloc(strlen(src.ptr) + 1); 94 | strcpy(dest.ptr , src.ptr); 95 | } 96 | 97 | 注意,shallow_copy可能会导致大量运行时错误,尤其是在对象创建和销毁时。使用浅拷贝时, 98 | 必须十分小心。在实际开发中,浅拷贝很少使用。大部分情况都应该使用深拷贝,特别是当需 99 | 要拷贝的结构很小时。 100 | 101 | ========================================================================================== 102 | 8.13.5 C语言的关键字“volatile”有何作用? 103 | 答: 104 | 关键字volatile的作用是指示编译器,即使代码不对变量做任何改动,该变量的值仍有可能会被 105 | 外界修改。操作系统、硬件或其他线程都有可能改变该变量。该变量的值有可能遭受意料之外的 106 | 修改。因此,每次使用时,编译器都会重新从内存中获取这个值。 107 | volatile(易变)的整数可由下面的语句声明: 108 | int volatile x; 109 | volatile int x; 110 | 要声明指向volatile整数的指针: 111 | volatile int *x; 112 | int volatile *x; 113 | 指向非volatile数据的volatile指针少见,但也是可行的: 114 | int *volatile x; 115 | 如若声明指向一块volatile内存的volatile指针变量(指针本身与地址所指内存都是volatile), 116 | 做法如下: 117 | 118 | ========================================================================================== 119 | 8.13.6 基类的析构函数为什么要声明为virtual? 120 | 答: 121 | 让我们先想想为何会有虚函数,假设有如下代码: 122 | class Foo{ 123 | public: 124 | void f(); 125 | }; 126 | 127 | class Bar : public Foo{ 128 | public: 129 | void f(); 130 | }; 131 | 132 | Foo *p = new Bar(); 133 | p->f(); 134 | 调用p->f()最后会调用Foo:f(),这是因为p是指向Foo的指针,而f()不是虚拟的。为确保p->f()会 135 | 调用继承关系最末端的子类的f()函数实现,我们需要将f()函数声明为虚函数。 136 | 现在,回到前面的析构函数。析构函数用于释放内存和资源。Foo的析构函数若不是虚拟的,那么即 137 | 使p实际上是Bar类型的,还是会调用Foo的析构函数。 138 | 这就是为何将析构函数声明为虚拟的原因 -- 确保正确调用继承关系最末端的子类的析构函数。 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /Cracking the Code Interview/8.2.1 deletedups.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.2.1 deletedups.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.2.2 kthToLast.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.2.2 kthToLast.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.2.3 deleteNode.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.2.3 deleteNode.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.2.4 partition.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.2.4 partition.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.2.5 addLists.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.2.5 addLists.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.2.5 addLists2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.2.5 addLists2.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.2.6 findBeginning.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.2.6 findBeginning.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.2.7 isPalindrome.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.2.7 isPalindrome.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.3.2 stackWithMin.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.3.2 stackWithMin.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.3.3 setOfStacks.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.3.3 setOfStacks.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.3.4 moveDisks.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.3.4 moveDisks.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.3.5 myQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.3.5 myQueue.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.3.6 stackSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.3.6 stackSort.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.3.7 animalQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.3.7 animalQueue.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.4.1 isBalance.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.4.1 isBalance.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.4.3 createMinimalBST.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.4.3 createMinimalBST.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.4.5 checkBST.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.4.5 checkBST.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.4.6 inOrderSucc.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.4.6 inOrderSucc.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.4.8 containsTree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.4.8 containsTree.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.5.1 updateBits.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.5.1 updateBits.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.5.2 doubleBinary.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.5.2 doubleBinary.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.5.5 bitSwapRequired.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.5.5 bitSwapRequired.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.7.3 lineIntersect.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.7.3 lineIntersect.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.7.4 computeWithAdd.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.7.4 computeWithAdd.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.9.1 countSteps.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.9.1 countSteps.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.9.2 countPaths.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.9.2 countPaths.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.9.3 magicIndex.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.9.3 magicIndex.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.9.4 getSubsets.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.9.4 getSubsets.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.9.5 permutationCombination.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.9.5 permutationCombination.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.9.6 generateParens.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.9.6 generateParens.cpp -------------------------------------------------------------------------------- /Cracking the Code Interview/8.9.8 makeChange.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/Cracking the Code Interview/8.9.8 makeChange.cpp -------------------------------------------------------------------------------- /HuaWeiOJ/1. LengthOfLastWord.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************ 2 | 描述 3 | 计算字符串最后一个单词的长度,单词以空格隔开。 4 | 知识点 字符串,循环 5 | 运行时间限制 0M 6 | 内存限制 0 7 | 输入 8 | 一行字符串,长度小于128。 9 | 输出 10 | 整数N,最后一个单词的长度。 11 | 样例输入 hello world 12 | 样例输出 5 13 | **************************************************************************/ 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | using namespace std; 20 | 21 | int main() 22 | { 23 | string str; 24 | while (getline(cin , str)) 25 | { 26 | if (str == "") 27 | cout << 0 << endl; 28 | else{ 29 | int len = strlen(str.c_str()), i; 30 | for (i = len - 1; str[i] != ' ' && i>0; i--) 31 | ; 32 | if (i == 0) 33 | cout << len; 34 | else 35 | cout << len - (i+1) << endl; 36 | }//else 37 | 38 | }//while 39 | system("pause"); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /HuaWeiOJ/2. HeChangDui.cpp: -------------------------------------------------------------------------------- 1 | /***************************************************************************************************** 2 | 描述 3 | 计算最少出列多少位同学,使得剩下的同学排成合唱队形 4 | 说明: 5 | N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形。 6 | 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2…,K,他们的身高分别为T1,T2,…,TK, 7 | 则他们的身高满足存在i(1<=i<=K)使得TiTi+1>......>TK。 8 | 你的任务是,已知所有N位同学的身高,计算最少需要几位同学出列,可以使得剩下的同学排成合唱队形。 9 | 10 | 知识点 循环 11 | 运行时间限制 0M 12 | 内存限制 0 13 | 输入 14 | 整数N 15 | 一行整数,空格隔开,N位同学身高 16 | 17 | 输出 18 | 最少需要几位同学出列 19 | 样例输入 8 186 186 150 200 160 130 197 200 20 | 样例输出 4 21 | **************************************************************************************************/ 22 | #include 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | void GetLeftList(const int *vh, int n , int *lv); 29 | void GetRightList(const int *vh, int n ,int *rv); 30 | int main() 31 | { 32 | int n , height; 33 | //其中vh存储整体身高序列,lv存储左起最长上升子序列,rv存储右起最长上升子序列 34 | int vh[500] = {0}, lv[500] = {0}, rv[500] = {0}; 35 | cin >> n; 36 | //如果人数小于等于2个,那么需要0人出队 37 | if (n <= 2) 38 | cout << 0 << endl; 39 | else{ 40 | for (int i = 0; i < n; i++) 41 | { 42 | cin >> height; 43 | vh[i] = height; 44 | }//for 45 | 46 | //求出左起升序序列 47 | GetLeftList(vh, n , lv); 48 | GetRightList(vh, n , rv); 49 | 50 | int tmp = 0, index; 51 | for (int i = 0; i < n; i++) 52 | if (tmp < lv[i] + rv[i]) 53 | { 54 | tmp = lv[i] + rv[i]; 55 | index = i; 56 | } 57 | 58 | cout << n - lv[index] - rv[index] + 1 << endl; 59 | }//else 60 | system("pause"); 61 | return 0; 62 | } 63 | 64 | void GetLeftList(const int *vh, int n, int *lv) 65 | { 66 | int i, j, k; 67 | for (i = 0; i < n; i++) 68 | { 69 | lv[i] = 1; 70 | for (j = 0; j < i; j++) 71 | if (vh[i]>vh[j] && lv[i] < (lv[j] + 1)) 72 | lv[i] = lv[j] + 1; 73 | } 74 | } 75 | 76 | void GetRightList(const int *vh, int n, int *rv) 77 | { 78 | int i, j, k; 79 | for (i = n - 1; i >= 0; i--) 80 | { 81 | rv[i] = 1; 82 | for (j = n - 1; j >i; j--) 83 | if (vh[i]>vh[j] && rv[i] < (rv[j] + 1)) 84 | rv[i] = rv[j] + 1; 85 | } 86 | } -------------------------------------------------------------------------------- /HuaWeiOJ/3. ImageSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/HuaWeiOJ/3. ImageSort.cpp -------------------------------------------------------------------------------- /HuaWeiOJ/4. NameBeauty.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/HuaWeiOJ/4. NameBeauty.cpp -------------------------------------------------------------------------------- /HuaWeiOJ/5. Choose 7.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/HuaWeiOJ/5. Choose 7.cpp -------------------------------------------------------------------------------- /HuaWeiOJ/6. StringEncrypt.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/HuaWeiOJ/6. StringEncrypt.cpp -------------------------------------------------------------------------------- /HuaWeiOJ/7. SnakeMatrix.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/HuaWeiOJ/7. SnakeMatrix.cpp -------------------------------------------------------------------------------- /HuaWeiOJ/8. StringEncrypt.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/HuaWeiOJ/8. StringEncrypt.cpp -------------------------------------------------------------------------------- /HuaWeiOJ/n1 lengthOfLastWord.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/HuaWeiOJ/n1 lengthOfLastWord.cpp -------------------------------------------------------------------------------- /HuaWeiOJ/n2 countChar.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/HuaWeiOJ/n2 countChar.cpp -------------------------------------------------------------------------------- /HuaWeiOJ/n3 randNum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/HuaWeiOJ/n3 randNum.cpp -------------------------------------------------------------------------------- /HuaWeiOJ/n4 partitionString.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 题目描述 3 | •连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组; 4 | •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。 5 | 输入描述: 6 | 连续输入字符串(输入2次,每个字符串长度小于100) 7 | 8 | 输出描述: 9 | 输出到长度为8的新字符串数组 10 | 11 | 输入例子: 12 | abc 13 | 123456789 14 | 15 | 输出例子: 16 | abc00000 17 | 12345678 18 | 90000000 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | vector partitionStr(string str) 29 | { 30 | if (str.empty()) 31 | return vector(); 32 | int len = str.length(); 33 | if (len % 8 != 0) 34 | { 35 | str += string(8 - len % 8, '0'); 36 | len += 8 - len % 8; 37 | }//if 38 | 39 | vector ret; 40 | for (int i = 0; i < len / 8; ++i) 41 | { 42 | ret.push_back(str.substr(8 * i, 8)); 43 | }//for 44 | 45 | return ret; 46 | } 47 | 48 | int main() 49 | { 50 | string str1, str2; 51 | cin >> str1 >> str2; 52 | 53 | vector ret1 = partitionStr(str1); 54 | for (auto iter = ret1.begin(); iter != ret1.end(); ++iter) 55 | { 56 | cout << *iter << endl; 57 | }//for 58 | 59 | vector ret2 = partitionStr(str2); 60 | for (auto iter = ret2.begin(); iter != ret2.end(); ++iter) 61 | { 62 | cout << *iter << endl; 63 | }//for 64 | 65 | system("pause"); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /JianZhi Offer/03_FindInPartiallySortedMatrix.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/03_FindInPartiallySortedMatrix.cpp -------------------------------------------------------------------------------- /JianZhi Offer/04_MergeArray.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/04_MergeArray.cpp -------------------------------------------------------------------------------- /JianZhi Offer/04_ReplaceBlank.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/04_ReplaceBlank.cpp -------------------------------------------------------------------------------- /JianZhi Offer/05_printListFromTailToHead.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/05_printListFromTailToHead.cpp -------------------------------------------------------------------------------- /JianZhi Offer/06_reConstructBinaryTree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/06_reConstructBinaryTree.cpp -------------------------------------------------------------------------------- /JianZhi Offer/07_StackToQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/07_StackToQueue.cpp -------------------------------------------------------------------------------- /JianZhi Offer/08_MinNumberInRotatedArray.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/08_MinNumberInRotatedArray.cpp -------------------------------------------------------------------------------- /JianZhi Offer/09_Fibonacci.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/09_Fibonacci.cpp -------------------------------------------------------------------------------- /JianZhi Offer/09_JumpFloor.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/09_JumpFloor.cpp -------------------------------------------------------------------------------- /JianZhi Offer/09_jumpFloorII.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/09_jumpFloorII.cpp -------------------------------------------------------------------------------- /JianZhi Offer/09_rectCover.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/09_rectCover.cpp -------------------------------------------------------------------------------- /JianZhi Offer/10_NumberOf1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/10_NumberOf1.cpp -------------------------------------------------------------------------------- /JianZhi Offer/11_Power.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/11_Power.cpp -------------------------------------------------------------------------------- /JianZhi Offer/12_Print1ToMaxOfNDigits.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/12_Print1ToMaxOfNDigits.cpp -------------------------------------------------------------------------------- /JianZhi Offer/13_DeleteNodeInList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/13_DeleteNodeInList.cpp -------------------------------------------------------------------------------- /JianZhi Offer/14_reOrderArray.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/14_reOrderArray.cpp -------------------------------------------------------------------------------- /JianZhi Offer/15_FindKthToTail.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/15_FindKthToTail.cpp -------------------------------------------------------------------------------- /JianZhi Offer/16_ReverseList.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/16_ReverseList.cpp -------------------------------------------------------------------------------- /JianZhi Offer/17_Merge.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/17_Merge.cpp -------------------------------------------------------------------------------- /JianZhi Offer/18_HasSubtree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/18_HasSubtree.cpp -------------------------------------------------------------------------------- /JianZhi Offer/19_Mirror.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/19_Mirror.cpp -------------------------------------------------------------------------------- /JianZhi Offer/20_printMatrix.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/20_printMatrix.cpp -------------------------------------------------------------------------------- /JianZhi Offer/21_MinInStack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/21_MinInStack.cpp -------------------------------------------------------------------------------- /JianZhi Offer/22_IsPopOrder.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/22_IsPopOrder.cpp -------------------------------------------------------------------------------- /JianZhi Offer/23_PrintFromTopToBottom.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/23_PrintFromTopToBottom.cpp -------------------------------------------------------------------------------- /JianZhi Offer/24_VerifySquenceOfBST.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/24_VerifySquenceOfBST.cpp -------------------------------------------------------------------------------- /JianZhi Offer/25_FindPath.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/25_FindPath.cpp -------------------------------------------------------------------------------- /JianZhi Offer/28_StringPermutation.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/28_StringPermutation.cpp -------------------------------------------------------------------------------- /JianZhi Offer/29_MoreThanHalfNum_Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题29 :数组中出现次数超过一半的数字 3 | * 数组中有一个数字出现的次数超过数组长度的一半, 4 | * 请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。 5 | * 由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。 6 | * 如果不存在则输出0。 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | class Solution { 16 | public: 17 | //方法一:排序,找中位数 18 | int MoreThanHalfNum_Solution1(vector numbers) { 19 | if (numbers.empty()) 20 | return 0; 21 | 22 | //求数组长度 23 | int size = numbers.size(); 24 | 25 | sort(numbers.begin(), numbers.end()); 26 | 27 | int ret = numbers[size / 2 - 1]; 28 | 29 | //检验出现次数是否超过数组长度的一半 30 | int count = 0; 31 | for (int i = 0; i < size; ++i) 32 | { 33 | if (numbers[i] == ret) 34 | ++count; 35 | }//for 36 | if (count > size / 2) 37 | return ret; 38 | else 39 | return 0; 40 | } 41 | 42 | //方法二,根据数组的特点找出O(n)的算法 43 | int MoreThanHalfNum_Solution(vector numbers) { 44 | if (numbers.empty()) 45 | return 0; 46 | /* 利用两个变量 一个ret存储数组中当前值,一个count保存次数 47 | * 当遍历下一个元素时,若与前一元素相同则次数+1 , 若不同则次数-1 , 当次数 = 0 时,保存下一个元素,并把次数 设为1 48 | * 由于我们要找的元素出现次数比其他所有元素出现的次数之和还多,所以要找的元素肯定是最后一次把次数设定为1的元素 49 | */ 50 | 51 | int ret = numbers[0], count = 1; 52 | int size = numbers.size(); 53 | for (int i = 1; i < size; ++i) 54 | { 55 | if (count == 0) 56 | { 57 | ret = numbers[i]; 58 | count = 1; 59 | continue; 60 | } 61 | else if (numbers[i] == ret) 62 | { 63 | ++count; 64 | } 65 | else{ 66 | --count; 67 | } 68 | }//for 69 | 70 | //检验出现次数是否超过数组长度的一半 71 | count = 0; 72 | for (int i = 0; i < size; ++i) 73 | { 74 | if (numbers[i] == ret) 75 | ++count; 76 | }//for 77 | if (count > size / 2) 78 | return ret; 79 | else 80 | return 0; 81 | 82 | } 83 | 84 | }; 85 | 86 | int main() 87 | { 88 | vector v = { 1, 2, 3, 2, 2, 2, 5, 4, 2 }; 89 | 90 | Solution s; 91 | cout << s.MoreThanHalfNum_Solution(v) << endl; 92 | 93 | system("pause"); 94 | return 0; 95 | } -------------------------------------------------------------------------------- /JianZhi Offer/30_GetLeastNumbers_Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题 30 最小的k个数 3 | * 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4, 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | class Solution { 15 | public: 16 | //方法一:使用stl库函数排序,然后返回前k个元素,复杂度为O(nlogn) 17 | vector GetLeastNumbers_Solution1(vector input, int k) { 18 | if (input.empty() || k <= 0 || k > input.size()) 19 | return vector(); 20 | 21 | int size = input.size(); 22 | 23 | if (k == size) 24 | return input; 25 | 26 | sort(input.begin(), input.end()); 27 | 28 | return vector(input.begin(), input.begin() + k); 29 | } 30 | 31 | //方法二:O(nlogk)的算法,特别适合处理海量数据 32 | vector GetLeastNumbers_Solution(vector input, int k) { 33 | if (input.empty() || k <= 0 || k > input.size()) 34 | return vector(); 35 | 36 | int size = input.size(); 37 | 38 | if (k == size) 39 | return input; 40 | 41 | //选择基于红黑树的multiset容器 42 | typedef multiset > intSet; 43 | typedef multiset::iterator setIter; 44 | 45 | intSet kNums; 46 | for (int i = 0; i < size; ++i) 47 | { 48 | if (kNums.size() < k) 49 | kNums.insert(input[i]); 50 | else{ 51 | //替换最大元素 52 | setIter iterMax = kNums.begin(); 53 | if (input[i] < *iterMax) 54 | { 55 | //删除最大 56 | kNums.erase(iterMax); 57 | kNums.insert(input[i]); 58 | }//if 59 | continue; 60 | } 61 | }//for 62 | vector ret(kNums.begin(), kNums.end()); 63 | reverse(ret.begin(), ret.end()); 64 | return ret; 65 | 66 | } 67 | }; 68 | 69 | int main() 70 | { 71 | Solution s; 72 | vector v = { 4, 5, 1, 6, 2, 7, 3, 8 }; 73 | vector ret = s.GetLeastNumbers_Solution(v, 4); 74 | 75 | system("pause"); 76 | return 0; 77 | } -------------------------------------------------------------------------------- /JianZhi Offer/31_FindGreatestSumOfSubArray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题 31 连续子数组的最大和 3 | 4 | * HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。 5 | * 今天测试组开完会后,他又发话了:在古老的一维模式识别中, 6 | * 常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。 7 | * 但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢? 8 | * 例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。 9 | * 你会不会被他忽悠住? 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | 18 | class Solution { 19 | public: 20 | int FindGreatestSumOfSubArray(vector array) { 21 | if (array.empty()) 22 | return 0; 23 | 24 | int maxSum = array[0], tmpSum = array[0] , size = array.size(); 25 | 26 | for (int i = 1; i < size; ++i) 27 | { 28 | tmpSum += array[i]; 29 | if (tmpSum > maxSum) 30 | maxSum = tmpSum; 31 | //如果当前最大和小于当前元素,改变子数组起点 32 | if (maxSum < array[i]) 33 | { 34 | maxSum = array[i]; 35 | tmpSum = array[i]; 36 | }else 37 | continue; 38 | }//for 39 | return maxSum; 40 | } 41 | }; 42 | 43 | 44 | int main() 45 | { 46 | Solution s; 47 | vector v = { 1, -2, 3, 10, -4, 7, 2, -5 }; 48 | 49 | cout << s.FindGreatestSumOfSubArray(v) << endl; 50 | 51 | system("pause"); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /JianZhi Offer/32_NumberOf1Between1AndN.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题32 :整数中1出现的次数(从1到n整数中1出现的次数) 3 | * 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数? 4 | * 为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次, 5 | * 但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化, 6 | * 可以很快的求出任意非负整数区间中1出现的次数。 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | class Solution { 17 | public: 18 | int NumberOf1Between1AndN_Solution(int n) 19 | { 20 | if (n < 0) 21 | return 0; 22 | 23 | char strN[50]; 24 | sprintf(strN, "%d", n); 25 | return NumberOf1(strN); 26 | } 27 | 28 | int NumberOf1(const char *strN) 29 | { 30 | if (!strN || *strN < '0' || *strN > '9' || *strN == '\0') 31 | return 0; 32 | 33 | int first = *strN - '0'; 34 | 35 | unsigned int length = static_cast(strlen(strN)); 36 | 37 | if (length == 1 && first == 0) 38 | return 0; 39 | 40 | if (length == 1 && first > 0) 41 | return 1; 42 | 43 | //假设strN是21345 44 | //numFirstDigit 是10000 - 19999中的第一个位中的数目 45 | int numFirstDigit = 0; 46 | if (first > 1) 47 | numFirstDigit = PowerBase10(length - 1); 48 | else if (first == 1) 49 | numFirstDigit = atoi(strN + 1) + 1; 50 | 51 | //numOtherDigits是1346 - 21345除了第一位之外的数位中的数目 52 | int numOtherDigits = first * (length - 1) * PowerBase10(length - 2); 53 | 54 | //numRecursive是1 - 1345中的数目 55 | int numRecursive = NumberOf1(strN + 1); 56 | 57 | return numFirstDigit + numOtherDigits + numRecursive; 58 | } 59 | 60 | int PowerBase10(unsigned int n) 61 | { 62 | int res = 1; 63 | for (unsigned int i = 0; i < n; ++i) 64 | res *= 10; 65 | return res; 66 | } 67 | }; 68 | 69 | int main() 70 | { 71 | Solution s; 72 | cout << s.NumberOf1Between1AndN_Solution(21345) << endl; 73 | 74 | system("pause"); 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /JianZhi Offer/33_PrintMinNumber.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题33:把数组排成最小的数 3 | * 输入一个正整数数组,把数组里所有数字拼接起来排成一个数, 4 | * 打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321}, 5 | * 则打印出这三个数字能排成的最小数字为321323。 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using namespace std; 16 | 17 | //自定义比较规则 18 | bool compare(const string &str1, const string &str2) 19 | { 20 | string s1 = str1 + str2, s2 = str2 + str1; 21 | return s1 < s2; 22 | } 23 | 24 | class Solution { 25 | public: 26 | string PrintMinNumber(vector numbers) { 27 | if (numbers.empty()) 28 | return string(); 29 | 30 | int size = numbers.size(); 31 | vector str(size, string()); 32 | for (int i = 0; i < size; ++i) 33 | { 34 | //将每个整数转换为字符串 35 | char tmp[21]; 36 | sprintf(tmp, "%d", numbers[i]); 37 | str[i] = tmp; 38 | }//for 39 | 40 | sort(str.begin(), str.end(), compare); 41 | 42 | //对于已排序的字符串序列直接连接即是组合成的最小整数 43 | string ret; 44 | for (int i = 0; i < size; ++i) 45 | { 46 | ret += str[i]; 47 | }//for 48 | return ret; 49 | } 50 | }; 51 | 52 | int main() 53 | { 54 | vector v = { 3, 32, 321 }; 55 | Solution s; 56 | 57 | cout << s.PrintMinNumber(v) << endl; 58 | 59 | system("pause"); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /JianZhi Offer/34_GetUglyNumber_Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目:面试题34 丑数 3 | * 把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 4 | * 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | class Solution { 16 | public: 17 | //方法一:逐个判断,时间复杂度高 18 | int GetUglyNumber_Solution1(int index) { 19 | if (index <= 0) 20 | return 0; 21 | if (index == 1) 22 | return 1; 23 | 24 | int count = 0 , number = 0; 25 | while (count < index) 26 | { 27 | ++number; 28 | 29 | if (isUgly(number)) 30 | ++count; 31 | }//while 32 | return number; 33 | } 34 | 35 | //判断丑数 36 | bool isUgly(int num) 37 | { 38 | while (num % 2 == 0) 39 | num /= 2; 40 | 41 | while (num % 3 == 0) 42 | num /= 3; 43 | 44 | while (num % 5 == 0) 45 | num /= 5; 46 | 47 | if (num == 1) 48 | return true; 49 | else 50 | return false; 51 | } 52 | 53 | //方法二:空间换时间 54 | int GetUglyNumber_Solution(int index) { 55 | if (index <= 0) 56 | return 0; 57 | else if (index == 1) 58 | return 1; 59 | 60 | vector uglyNums(index, 0); 61 | uglyNums[0] = 1; 62 | int count = 1; 63 | vector::iterator iter1, iter2, iter3; 64 | iter1 = uglyNums.begin(); 65 | iter2 = uglyNums.begin(); 66 | iter3 = uglyNums.begin(); 67 | 68 | while (count < index) 69 | { 70 | int tmp = min(min(*iter1 * 2, *iter2 * 3), *iter3 * 5); 71 | uglyNums[count] = tmp; 72 | 73 | //找到下一个* 2、3、5值大于目前最后一个丑数的丑数 74 | while (*iter1 * 2 <= uglyNums[count]) 75 | iter1++; 76 | 77 | while (*iter2 * 3 <= uglyNums[count]) 78 | iter2++; 79 | 80 | while (*iter3 * 5 <= uglyNums[count]) 81 | iter3++; 82 | //当前丑数序列数量加1 83 | ++count; 84 | }//while 85 | return uglyNums[count - 1]; 86 | } 87 | 88 | }; 89 | 90 | int main() 91 | { 92 | Solution s; 93 | cout << s.GetUglyNumber_Solution(5) << endl; 94 | 95 | system("pause"); 96 | return 0; 97 | } -------------------------------------------------------------------------------- /JianZhi Offer/35_FirstNotRepeatingChar.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题35:第一个只出现一次的字符位置 3 | * 描述:在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符的位置。 4 | * 若为空串,返回-1。位置索引从0开始。 5 | */ 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | class Solution { 14 | public: 15 | //方法一:二次遍历 16 | int FirstNotRepeatingChar1(string str) { 17 | if (str.empty()) 18 | return -1; 19 | 20 | int size = str.length(); 21 | if (size == 1) 22 | return 0; 23 | 24 | vector visited(size,0); 25 | 26 | for (int i = 0; i < size; ++i) 27 | { 28 | if (visited[i] == 1) 29 | continue; 30 | //标记为已访问 31 | visited[i] = 1; 32 | //向后查找 33 | int j; 34 | for (j = i + 1; j < size; ++j) 35 | { 36 | if (visited[j] == 1) 37 | continue; 38 | if (str[j] == str[i]) 39 | break; 40 | } 41 | //找到第一个只出现一次的字符 42 | if (j == size) 43 | { 44 | return i; 45 | }//if 46 | else{ 47 | //说明该字符是重复字符,将所有的该字符都标记为访问过 48 | for (j = i + 1; j < size; ++j) 49 | { 50 | if (str[j] == str[i]) 51 | visited[j] = 1; 52 | }//for 53 | } 54 | }//for 55 | return -1; 56 | } 57 | 58 | //方法二:利用哈希 59 | int FirstNotRepeatingChar(string str) { 60 | if (str.empty()) 61 | return -1; 62 | 63 | int size = str.length(); 64 | if (size == 1) 65 | return 0; 66 | 67 | const int MAX_LENGTH = 256; 68 | vector hashTable(MAX_LENGTH, 0); 69 | 70 | string::iterator iter = str.begin(); 71 | while (iter != str.end()) 72 | ++hashTable[*(iter++)]; 73 | 74 | iter = str.begin(); 75 | while (iter != str.end()) 76 | { 77 | if (hashTable[*iter] == 1) 78 | return iter - str.begin(); 79 | ++iter; 80 | } 81 | return -1; 82 | } 83 | }; 84 | 85 | int main() 86 | { 87 | string str = "Hello , Hey , I am a good student"; 88 | 89 | Solution s; 90 | 91 | cout << s.FirstNotRepeatingChar(str) << endl; 92 | 93 | system("pause"); 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /JianZhi Offer/38_GetNumberOfk.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题38:数字在排序数组中出现的次数 3 | * 描述:统计一个数字在排序数组中出现的次数。 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | class Solution { 13 | public: 14 | //二分法最差时间复杂度O(n) 15 | int GetNumberOfK1(vector data, int k) { 16 | if (data.empty()) 17 | return 0; 18 | 19 | int lhs = 0, rhs = data.size() - 1, pos = -1; 20 | 21 | while (lhs <= rhs) 22 | { 23 | pos = (lhs + rhs) / 2; 24 | if (data[pos] == k) 25 | break; 26 | else if (data[pos] < k) 27 | { 28 | lhs = pos + 1; 29 | } 30 | else{ 31 | rhs = pos - 1; 32 | }//else 33 | }//while 34 | //若目标元素不存在 35 | if (pos == -1 || lhs > rhs) 36 | return 0; 37 | else{ 38 | int count = 1; 39 | int i = pos - 1, j = pos + 1; 40 | while (i >= 0 && data[i] == k) 41 | { 42 | ++count; 43 | --i; 44 | } 45 | 46 | while (j < data.size() && data[j] == k) 47 | { 48 | ++count; 49 | ++j; 50 | } 51 | 52 | return count; 53 | }//else 54 | } 55 | //方法二:使用二分查找得到第一个和最后一个k出现的位置 56 | int GetNumberOfK(vector data, int k) { 57 | if (data.empty()) 58 | return 0; 59 | int first = getFirstK(data, k); 60 | int last = getLastK(data, k); 61 | 62 | if (first != -1 && last != -1) 63 | return last - first + 1; 64 | return 0; 65 | } 66 | 67 | int getFirstK(vector data, int k) 68 | { 69 | if (data.empty()) 70 | return -1; 71 | 72 | int pos = -1, lhs = 0, rhs = data.size() - 1; 73 | while (lhs <= rhs) 74 | { 75 | pos = (lhs + rhs) / 2; 76 | if (data[pos] == k) 77 | { 78 | //出现在首尾或者是第一个k时直接返回 79 | if ((pos == 0) || (pos > 0 && data[pos - 1] != k)) 80 | return pos; 81 | else 82 | rhs = pos - 1; 83 | }//if 84 | else if (data[pos] < k) 85 | { 86 | lhs = pos + 1; 87 | } 88 | else{ 89 | rhs = pos - 1; 90 | } 91 | }//while 92 | return -1; 93 | } 94 | int getLastK(vector data, int k) 95 | { 96 | if (data.empty()) 97 | return -1; 98 | 99 | int pos = -1, lhs = 0, rhs = data.size() - 1; 100 | while (lhs <= rhs) 101 | { 102 | pos = (lhs + rhs) / 2; 103 | if (data[pos] == k) 104 | { 105 | //出现在首尾或者是第一个k时直接返回 106 | if ((pos == data.size() - 1) || (pos < data.size()-1 && data[pos + 1] != k)) 107 | return pos; 108 | else 109 | lhs = pos + 1; 110 | }//if 111 | else if (data[pos] < k) 112 | { 113 | lhs = pos + 1; 114 | } 115 | else{ 116 | rhs = pos - 1; 117 | } 118 | }//while 119 | return -1; 120 | } 121 | }; 122 | 123 | int main() 124 | { 125 | Solution s; 126 | vector data = { 1, 2, 3, 4, 5, 5, 5, 6, 7 }; 127 | cout << s.GetNumberOfK(data , 5) << endl; 128 | 129 | system("pause"); 130 | return 0; 131 | } -------------------------------------------------------------------------------- /JianZhi Offer/39_IsBalanced_Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题39 平衡二叉树 3 | * 题目二 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 4 | */ 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | /* 10 | struct TreeNode { 11 | int val; 12 | struct TreeNode *left; 13 | struct TreeNode *right; 14 | TreeNode(int x) : 15 | val(x), left(NULL), right(NULL) { 16 | } 17 | };*/ 18 | 19 | struct TreeNode{ 20 | int val; 21 | struct TreeNode *left; 22 | struct TreeNode *right; 23 | TreeNode(int x) :val(x), left(NULL), right(NULL){} 24 | }; 25 | 26 | class Solution { 27 | public: 28 | //求深度 29 | int TreeDepth(TreeNode* pRoot) 30 | { 31 | if (!pRoot) 32 | return 0; 33 | int dLeft = TreeDepth(pRoot->left); 34 | int dRight = TreeDepth(pRoot->right); 35 | return (dLeft > dRight) ? (dLeft + 1) : (dRight + 1); 36 | } 37 | 38 | //方法一:需要重复遍历多次 39 | bool IsBalanced_Solution1(TreeNode* pRoot) { 40 | if (!pRoot) 41 | return true; 42 | 43 | int dLeft = TreeDepth(pRoot->left); 44 | int dRight = TreeDepth(pRoot->right); 45 | 46 | if (abs(dLeft - dRight) > 1) 47 | return false; 48 | else{ 49 | return IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right); 50 | } 51 | } 52 | 53 | //方法二:每个节点只需要遍历一次 54 | bool IsBalanced_Solution(TreeNode* pRoot) 55 | { 56 | int depth = 0; 57 | return ISBalanced(pRoot, depth); 58 | } 59 | 60 | bool ISBalanced(TreeNode *root, int &depth) 61 | { 62 | if (!root) 63 | { 64 | depth = 0; 65 | return true; 66 | } 67 | 68 | int dLeft, dRight; 69 | if (ISBalanced(root->left, dLeft) && ISBalanced(root->right, dRight)) 70 | { 71 | if (abs(dLeft - dRight) <= 1) 72 | { 73 | depth = 1 + (dLeft > dRight ? dLeft : dRight); 74 | return true; 75 | } 76 | }//if 77 | return false; 78 | } 79 | }; -------------------------------------------------------------------------------- /JianZhi Offer/39_TreeDepth.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题39 求二叉树的深度 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | /* 10 | struct TreeNode { 11 | int val; 12 | struct TreeNode *left; 13 | struct TreeNode *right; 14 | TreeNode(int x) : 15 | val(x), left(NULL), right(NULL) { 16 | } 17 | };*/ 18 | 19 | struct TreeNode{ 20 | int val; 21 | struct TreeNode *left; 22 | struct TreeNode *right; 23 | TreeNode(int x) :val(x), left(NULL), right(NULL){} 24 | }; 25 | class Solution { 26 | public: 27 | int TreeDepth(TreeNode* pRoot) 28 | { 29 | if (!pRoot) 30 | return 0; 31 | int dLeft = TreeDepth(pRoot->left); 32 | int dRight = TreeDepth(pRoot->right); 33 | return (dLeft > dRight) ? (dLeft + 1) : (dRight + 1); 34 | } 35 | }; -------------------------------------------------------------------------------- /JianZhi Offer/40_FindNumsAppearOnce.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/40_FindNumsAppearOnce.cpp -------------------------------------------------------------------------------- /JianZhi Offer/41_FindNumbersWithSum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题41 :和为s的两个数字 3 | * 输入一个递增排序的数组和一个数字S,在数组中查找两个数, 4 | * 使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | class Solution { 14 | public: 15 | //方法一:二次遍历 16 | vector FindNumbersWithSum(vector array, int sum) { 17 | if (array.empty()) 18 | return vector(); 19 | 20 | int sz = array.size(); 21 | vector ret; 22 | 23 | int multi = INT_MAX; 24 | for (int i = 0; i < sz; ++i) 25 | { 26 | for (int j = i + 1; j < sz; ++j) 27 | { 28 | if (array[i] + array[j] == sum) 29 | { 30 | if (array[i] * array[j] < multi) 31 | { 32 | multi = array[i] * array[j]; 33 | ret.clear(); 34 | ret.push_back(array[i]); 35 | ret.push_back(array[j]); 36 | }//if 37 | }//if 38 | }//for 39 | }//for 40 | return ret; 41 | } 42 | }; 43 | 44 | int main() 45 | { 46 | Solution s; 47 | vector v = { 1, 2, 3, 4, 11, 15 }; 48 | vector ret = s.FindNumbersWithSum(v , 15); 49 | 50 | if (!ret.empty()) 51 | cout << ret[0] << "\t" << ret[1] << endl; 52 | 53 | system("pause"); 54 | return 0; 55 | } -------------------------------------------------------------------------------- /JianZhi Offer/42_LeftRotateString.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目:面试题42 左旋转字符串 3 | * 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务, 4 | * 就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S, 5 | * 请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”, 6 | * 要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它! 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | class Solution { 16 | public: 17 | string LeftRotateString(string str, int n) { 18 | if (str.empty() || n == 0) 19 | return str; 20 | 21 | n %= str.size(); 22 | 23 | string::iterator beg = str.begin(); 24 | reverse(beg, beg + n); 25 | reverse(beg + n, str.end()); 26 | reverse(beg, str.end()); 27 | 28 | return str; 29 | } 30 | }; 31 | 32 | int main() 33 | { 34 | Solution s; 35 | 36 | string str = "abcdefg"; 37 | str = s.LeftRotateString(str, 2); 38 | cout << str << endl; 39 | 40 | system("pause"); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /JianZhi Offer/42_ReverseSentence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 题目: 面试题42 反转单词顺序 3 | * 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。 4 | * 同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。 5 | * 例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了, 6 | * 正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么? 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | class Solution { 16 | public: 17 | //两次反转的方法 18 | string ReverseSentence(string str) { 19 | if (str.empty()) 20 | return str; 21 | //第一步,反转整个字符串 22 | reverse(str.begin(), str.end()); 23 | 24 | //第二步,反转每个单词 25 | string::iterator iter,beg = str.begin(); 26 | 27 | for (iter = str.begin(); iter != str.end(); ++iter) 28 | { 29 | if (*iter == ' ') 30 | { 31 | reverse(beg, iter); 32 | beg = iter + 1; 33 | }//if 34 | }//for 35 | reverse(beg, str.end()); 36 | return str; 37 | } 38 | }; 39 | 40 | int main() 41 | { 42 | Solution s; 43 | 44 | string str = "I am a student."; 45 | str = s.ReverseSentence(str); 46 | cout << str << endl; 47 | 48 | system("pause"); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /JianZhi Offer/46_Sum_Solution.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题 46 求1+2+3 + ... + n 3 | * 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Sum{ 12 | public: 13 | Sum(){ 14 | ++n; 15 | sum += n; 16 | } 17 | 18 | static void clear() 19 | { 20 | n = 0; 21 | sum = 0; 22 | } 23 | 24 | static int getSum() 25 | { 26 | return sum; 27 | } 28 | private: 29 | static unsigned int n; 30 | static unsigned int sum; 31 | }; 32 | 33 | unsigned Sum::n = 0; 34 | unsigned Sum::sum = 0; 35 | 36 | class Solution { 37 | public: 38 | //方法一:利用构造函数求解 39 | 40 | int Sum_Solution(int n) { 41 | Sum::clear(); 42 | Sum *s = new Sum[n]; 43 | delete[]s; 44 | s = NULL; 45 | 46 | return Sum::getSum(); 47 | } 48 | }; 49 | 50 | int main() 51 | { 52 | Solution s; 53 | 54 | cout << s.Sum_Solution(10) << endl; 55 | 56 | system("pause"); 57 | return 0; 58 | } -------------------------------------------------------------------------------- /JianZhi Offer/50_getLastCommonParent.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/50_getLastCommonParent.cpp -------------------------------------------------------------------------------- /JianZhi Offer/51_duplicate.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/51_duplicate.cpp -------------------------------------------------------------------------------- /JianZhi Offer/52_multiply.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题52:构建乘积数组 3 | * 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | class Solution { 13 | public: 14 | vector multiply(const vector& A) { 15 | if (A.empty()) 16 | return vector(); 17 | 18 | int n = A.size(); 19 | //第一步,每个元素位置i求得A[0]*A[1]*...*A[i-1] 20 | vector ret(n, 1); 21 | ret[0] = 1; 22 | for (int i = 1; i < n; ++i) 23 | { 24 | ret[i] = ret[i - 1] * A[i - 1]; 25 | }//for 26 | 27 | //第二步,ret[i] *= A[i+1]*...A[n-1] 28 | int tmp = 1; 29 | for (int j = n - 2; j >= 0; --j) 30 | { 31 | tmp *= A[j + 1]; 32 | ret[j] *= tmp; 33 | }//for 34 | 35 | return ret; 36 | } 37 | }; 38 | 39 | int main() 40 | { 41 | Solution s; 42 | vector A = { 1, 2, 3, 4, 5, 6 }; 43 | vector B = s.multiply(A); 44 | 45 | for (unsigned i = 0; i < B.size(); ++i) 46 | { 47 | cout << B[i] << "\t"; 48 | }//for 49 | system("pause"); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /JianZhi Offer/53_match.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题53 正则表达式匹配 3 | * 请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符, 4 | * 而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。 5 | * 例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | class Solution { 15 | public: 16 | bool match(char* str, char* pattern) 17 | { 18 | if (str == NULL || pattern == NULL) 19 | return false; 20 | 21 | return judge(str, pattern); 22 | } 23 | 24 | bool judge(char *str, char *pattern) 25 | { 26 | if (*str == '\0' && *pattern == '\0') 27 | return true; 28 | else if (*str != '\0' && *pattern == '\0') 29 | return false; 30 | else{ 31 | if (*(pattern + 1) == '*') 32 | { 33 | if (*str == *pattern || (*pattern == '.' && *str != '\0')) 34 | return judge(str + 1, pattern + 2) || judge(str + 1, pattern) || judge(str, pattern + 2); 35 | else 36 | return judge(str, pattern + 2); 37 | } 38 | else if (*str == *pattern || (*pattern == '.' && *str != '\0')) 39 | return judge(str + 1, pattern + 1); 40 | return false; 41 | }//else 42 | } 43 | }; 44 | 45 | int main() 46 | { 47 | Solution s; 48 | cout << s.match("aaa", "a.a") << endl; 49 | cout << s.match("aaa", "ab*ac*a") << endl; 50 | system("pause"); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /JianZhi Offer/54_isNumeric.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/54_isNumeric.cpp -------------------------------------------------------------------------------- /JianZhi Offer/55_firstAppearingOne.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/JianZhi Offer/55_firstAppearingOne.cpp -------------------------------------------------------------------------------- /JianZhi Offer/58_GetNext.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题58:二叉树的下一个结点 3 | * 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。 4 | * 注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | using namespace std; 13 | /* 14 | struct TreeLinkNode { 15 | int val; 16 | struct TreeLinkNode *left; 17 | struct TreeLinkNode *right; 18 | struct TreeLinkNode *next; 19 | TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) { 20 | 21 | } 22 | }; 23 | */ 24 | 25 | struct TreeLinkNode { 26 | int val; 27 | struct TreeLinkNode *left; 28 | struct TreeLinkNode *right; 29 | struct TreeLinkNode *next; 30 | 31 | TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL){} 32 | }; 33 | 34 | class Solution { 35 | public: 36 | TreeLinkNode* GetNext(TreeLinkNode* pNode) 37 | { 38 | if (!pNode) 39 | return NULL; 40 | 41 | // 1)如果pNode包含右子树,则右子树中的最左结点为其中序遍历的下一个结点 42 | if (pNode->right != NULL) 43 | { 44 | TreeLinkNode *node = pNode->right; 45 | while (node->left) 46 | { 47 | node = node->left; 48 | }//while 49 | return node; 50 | } 51 | // 2)没有右子树,且父节点不为空 52 | else if (pNode->right == NULL && pNode->next != NULL) 53 | { 54 | // 3)当前节点为其父节点的左子树,则其父节点便是中序遍历的下一个节点 55 | if (pNode == pNode->next->left) 56 | { 57 | return pNode->next; 58 | } 59 | else{ 60 | TreeLinkNode *pCurrent = pNode, *pParent = pNode->next; 61 | while (pParent != NULL && pCurrent == pParent->right) 62 | { 63 | pCurrent = pParent; 64 | pParent = pParent->next; 65 | }//while 66 | 67 | return pParent; 68 | } 69 | 70 | } 71 | //父节点为空,且没有右子树 72 | else{ 73 | return NULL; 74 | } 75 | } 76 | }; 77 | 78 | -------------------------------------------------------------------------------- /JianZhi Offer/59_isSymmetrical.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 面试题59:对称的二叉树 3 | * 请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | /* 12 | struct TreeNode { 13 | int val; 14 | struct TreeNode *left; 15 | struct TreeNode *right; 16 | TreeNode(int x) : 17 | val(x), left(NULL), right(NULL) { 18 | } 19 | }; 20 | */ 21 | 22 | struct TreeNode{ 23 | int val; 24 | struct TreeNode *left; 25 | struct TreeNode *right; 26 | TreeNode(int x) :val(x), left(NULL), right(NULL){} 27 | }; 28 | class Solution { 29 | public: 30 | bool isSymmetrical(TreeNode* pRoot) 31 | { 32 | if (!pRoot) 33 | return true; 34 | 35 | return isSymmetrical(pRoot->left, pRoot->right); 36 | } 37 | 38 | bool isSymmetrical(TreeNode *pRoot1, TreeNode *pRoot2) 39 | { 40 | if (pRoot1 == NULL && pRoot2 == NULL) 41 | return true; 42 | else if (pRoot1 == NULL || pRoot2 == NULL) 43 | return false; 44 | else{ 45 | if (pRoot1->val != pRoot2->val) 46 | return false; 47 | else{ 48 | return isSymmetrical(pRoot1->left, pRoot2->right) && isSymmetrical(pRoot1->right, pRoot2->left); 49 | } 50 | } 51 | return true; 52 | } 53 | 54 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Offer2016 2 | 集中《剑指Offer》《程序员面试金典》经典算法问题 3 | -------------------------------------------------------------------------------- /sorts/BubbleSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/sorts/BubbleSort.cpp -------------------------------------------------------------------------------- /sorts/HeapSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/sorts/HeapSort.cpp -------------------------------------------------------------------------------- /sorts/InsertionSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/sorts/InsertionSort.cpp -------------------------------------------------------------------------------- /sorts/MergeSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/sorts/MergeSort.cpp -------------------------------------------------------------------------------- /sorts/QuickSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/sorts/QuickSort.cpp -------------------------------------------------------------------------------- /sorts/SelectionSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/sorts/SelectionSort.cpp -------------------------------------------------------------------------------- /sorts/ShellSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shineyr/Offer2016/85e2396d9711b5f55ec3cd9470645953800f2912/sorts/ShellSort.cpp --------------------------------------------------------------------------------