├── template ├── out.txt ├── in.txt ├── template.cpp ├── oj.c └── oj.cpp ├── 026-复杂链表的复制 ├── 1.jpg ├── 2.jpg ├── 3.jpg ├── 4.jpg ├── map.cpp ├── linknext.cpp └── bruteforce.cpp ├── 028-字符串的排列 ├── abc.jpg ├── next_permutation.cpp ├── permutation_error.cpp └── permutation.cpp ├── 031-连续子数组的最大和 ├── dp.png ├── 03-sum.cpp ├── 04-sum.cpp ├── 01-bruteforce.cpp └── 02-dp.cpp ├── 003-二维数组中的查找 ├── find.jpg ├── find7.jpg ├── bruteforce.cpp ├── divide-leftdown.cpp └── divide-rightup.cpp ├── 006-重建二叉树 └── construct.cpp ├── 007-用两个栈实现队列 ├── README.md ├── stack2queue.cpp └── stack2queue.jpg ├── 010-二进制中1的个数 ├── bitset.cpp ├── numberof1_1.cpp ├── numberof1_2.cpp ├── numberof1_3.cpp └── numberof1_4.cpp ├── 011-数值的整数次方 ├── power_1.cpp ├── power_2.cpp ├── power_3.cpp └── power_4.cpp ├── 017-合并两个排序的链表 ├── merge.cpp └── merge1.cpp ├── 019-二叉树的镜像 ├── mirror.jpg ├── mirror_in.cpp ├── mirror_pre.cpp ├── mirror_re.cpp └── mirror_post.cpp ├── 021-包含min函数的栈 ├── README.md └── stack.cpp ├── 023-从上往下打印二叉树 ├── README.md └── levelorder.cpp ├── 030-最小的K个数 ├── 01-sort.cpp ├── 02-heap.cpp ├── 03-partition.cpp └── 04-multiset.cpp ├── 034-丑数 ├── 02-uglynum.cpp ├── 01-bruteforce.cpp └── 02-uglynum_set.cpp ├── 041-和为S的连续正数序列 └── find.cpp ├── 058-二叉树的下一个结点 └── bst.png ├── 064-数据流之中的中位数 ├── multiset └── multiset.cpp ├── 004-替换空格 ├── replacespace.cpp └── replacespace2.cpp ├── 009-斐波那契数列 ├── jumpFloor.cpp ├── rectCover.cpp ├── fibonacci_1.cpp ├── fibonacci_2.cpp ├── jumpFloorII-2.cpp ├── jumpFloorII-3.cpp ├── jumpFloorII.cpp └── fibonacci_logn.cpp ├── 016-反转链表 ├── reverselist1.cpp └── reverselist2.cpp ├── 018-树的子结构 └── hassubtree.cpp ├── 024-二叉搜索树的后序遍历序列 ├── tree.jpg ├── ispost_max ├── ispost_n.cpp ├── ispost_n2.cpp ├── ispost_re.cpp ├── ispost_max.cpp └── bruteforce.cpp ├── 033-把数组排成最小的数 └── minnum.cpp ├── 039-二叉树的深度 └── treedepth.cpp ├── 048-不能被继承的类 ├── friend_class ├── static_func.cpp ├── friend_fun.cpp ├── friend_class.cpp ├── final.cpp └── static_value.cpp ├── 005-从尾到头打印链表 ├── print_stack.cpp ├── print_recursion.cpp └── print_recursion2.cpp ├── 008-旋转数组的最小数字 ├── minnumber.cpp ├── minnumber-On1.cpp └── minnumber-On2.cpp ├── 012-打印1到最大的N位数 ├── print_string ├── print_string.cpp ├── print_recursion.cpp ├── print_recursion2.cpp └── print.cpp ├── 015-链表中倒数第k个结点 ├── findkth1.cpp └── findkth2.cpp ├── 020-顺时针打印矩阵 ├── printmartix.cpp └── printmartix2.cpp ├── 022-栈的压入弹出序列 ├── ispoporder1.cpp ├── ispoporder2.cpp ├── ispoporder3.cpp └── ispoporder4.py ├── 037-两个链表的第一个公共结点 ├── 03-map.cpp ├── 01-bruteforce.cpp ├── 02-align_stack.cpp ├── 02-align_length.cpp └── 02-align_strcat.cpp ├── 014-调整数组顺序使奇数位于偶数前面 ├── buttle.cpp ├── otherarray.cpp └── reOrderArray.cpp ├── 027-二叉搜索树与双向链表 ├── convert_re.cpp ├── convert_re2.cpp ├── convert_stack.cpp └── convert_re_static_error.cpp ├── 035-第一个只出现一次的字符位置 ├── 03-count.cpp ├── 02-bitmap.cpp └── 01-bruteforce.cpp ├── 046-求1+2+3+...+n ├── bitmulti.cpp ├── pointerofarray.c ├── functemplate.cpp ├── recursion.cpp ├── macro.cpp ├── funcpoint.cpp ├── virtualfunc.cpp └── constructor.cpp ├── 029-数组中出现次数超过一半的数字 ├── 01-halfnum.cpp ├── 02-partition.cpp ├── 03-holdfield.cpp ├── 04-stlcount.cpp └── 05-map.cpp ├── 032-从1到n整数中1出现的次数 ├── 02-divide.cpp ├── 02-divide2.cpp ├── 01-bruteforce.cpp └── 03-num.cpp ├── 025-二叉树中和为某一值的路径 ├── findpath_left_arg.cpp ├── findpath_sum_arg.cpp ├── findpath_sum_static.cpp └── findpath_left_static.cpp ├── 061-按之字形顺序打印二叉树 ├── levelorder_deque.cpp └── levelorder_reverse.cpp ├── ChineseCodingInterviewAppendix-master ├── ArrayConstruction │ ├── ArrayConstruction.cpp │ └── ArrayConstruction.vcxproj.filters ├── Utilities │ ├── Utilities.vcxproj.user │ ├── list.h │ ├── BinaryTree.h │ ├── Utilities.vcxproj.filters │ ├── BinaryTree.cpp │ └── list.cpp ├── FirstCharacterInStream │ ├── FirstCharacterInStream.cpp │ └── FirstCharacterInStream.vcxproj.filters ├── RegularExpressionsMatching │ ├── RegularExpressions.cpp │ └── RegularExpressionsMatching.vcxproj.filters ├── README.md ├── RobotMove │ ├── RobotMove.vcxproj.filters │ └── RobotMove.cpp ├── KthNodeInBST │ └── KthNodeInBST.vcxproj.filters ├── StreamMedian │ └── StreamMedian.vcxproj.filters ├── NumericStrings │ ├── NumericStrings.vcxproj.filters │ └── NumericString.cpp ├── StringPathInMatrix │ └── StringPathInMatrix.vcxproj.filters ├── DuplicationInArray │ └── DuplicationInArray.vcxproj.filters ├── NextNodeInBinaryTrees │ └── NextNodeInBinaryTrees.vcxproj.filters ├── PrintTreesInLines │ └── PrintTreesInLines.vcxproj.filters ├── EntryNodeInListLoop │ └── EntryNodeInListLoop.vcxproj.filters ├── MaxInSlidingWindow │ └── MaxInSlidingWindow.vcxproj.filters ├── PrintTreesInZigzag │ └── PrintTreesInZigzag.vcxproj.filters ├── SerializeBinaryTrees │ └── SerializeBinaryTrees.vcxproj.filters ├── DeleteDuplicatedListNode │ └── DeleteDuplicatedListNode.vcxproj.filters └── SymmetricalBinaryTree │ └── SymmetricalBinaryTree.vcxproj.filters ├── 053-正则表达式匹配 ├── re.py ├── dfs2.cpp ├── dp.cpp └── dfs.cpp ├── .gitignore ├── 047-不用加减乘除做加法 ├── inline_asm.cpp └── bit_add.cpp ├── 038-数字在排序数组中出现的次数 ├── 03-firstlast_stlbound.cpp ├── 04-multiset.cpp ├── 01-bruteforce.cpp ├── 03-firstlast_recursion.cpp ├── 02-binarysearch.cpp └── 03-firstlast.cpp ├── 042-左旋转字符串 ├── 01-rotate2.cpp ├── 02-reverse2.cpp ├── 02-reverse.cpp └── 01-rotate.cpp ├── 052-构建乘积数组 ├── multiply.cpp └── README.md ├── 042-翻转单词顺序列 ├── 04-reverse_temp.cpp ├── 04-reverse_temp2.cpp ├── 02-reverse_stl.cpp ├── 01-reverse.cpp └── 03-reverse_stack.cpp ├── 036-数组中的逆序对 ├── 01-bruteforce.cpp ├── 03-selectsort.cpp ├── 02-bubblesort.cpp ├── 05-hash.cpp └── 04-mergedivide.cpp ├── 045-孩子们的游戏(圆圈中最后剩下的数) ├── joseph_recursion.cpp ├── joseph.cpp └── lastremaining.cpp ├── 041-和为S的两个数字 └── find.cpp ├── 065-滑动窗口的最大值 ├── heap1.cpp ├── bruteforce.cpp ├── heap.cpp ├── maxqueue_s.cpp └── maxqueue.cpp ├── 056-链表中环的入口结点 └── entry2.cpp ├── 055-字符流中第一个不重复的字符 └── count.cpp ├── 062-序列化二叉树 ├── string_to_int.cpp └── int_to_string.cpp ├── 040-数组中只出现一次的数字 ├── find2.cpp └── find.cpp ├── 044-扑克牌顺子 ├── continuous_sort.cpp └── continuous_n.cpp ├── 051-数组中重复的数字 ├── flag.cpp ├── addlength.cpp ├── sort.cpp ├── hashset.cpp ├── 0flag.cpp └── swap.cpp ├── 057-删除链表中重复的结点 ├── count.cpp └── delete.cpp ├── 054-表示数值的字符串 └── isnumeric.cpp ├── 059-对称的二叉树 └── recursion.cpp ├── 063-二叉搜索树的第K个结点 ├── recursion.cpp └── inorder_stack.cpp ├── 049-把字符串转换成整数 └── myatoi.cpp └── 039-平衡二叉树[附加] └── isavlrecursion.cpp /template/out.txt: -------------------------------------------------------------------------------- 1 | 8 2 | 3 3 | -------------------------------------------------------------------------------- /template/in.txt: -------------------------------------------------------------------------------- 1 | 3 5 2 | 1 2 3 | 4 | -------------------------------------------------------------------------------- /026-复杂链表的复制/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/026-复杂链表的复制/1.jpg -------------------------------------------------------------------------------- /026-复杂链表的复制/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/026-复杂链表的复制/2.jpg -------------------------------------------------------------------------------- /026-复杂链表的复制/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/026-复杂链表的复制/3.jpg -------------------------------------------------------------------------------- /026-复杂链表的复制/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/026-复杂链表的复制/4.jpg -------------------------------------------------------------------------------- /028-字符串的排列/abc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/028-字符串的排列/abc.jpg -------------------------------------------------------------------------------- /026-复杂链表的复制/map.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/026-复杂链表的复制/map.cpp -------------------------------------------------------------------------------- /031-连续子数组的最大和/dp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/031-连续子数组的最大和/dp.png -------------------------------------------------------------------------------- /003-二维数组中的查找/find.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/003-二维数组中的查找/find.jpg -------------------------------------------------------------------------------- /003-二维数组中的查找/find7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/003-二维数组中的查找/find7.jpg -------------------------------------------------------------------------------- /006-重建二叉树/construct.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/006-重建二叉树/construct.cpp -------------------------------------------------------------------------------- /007-用两个栈实现队列/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/007-用两个栈实现队列/README.md -------------------------------------------------------------------------------- /010-二进制中1的个数/bitset.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/010-二进制中1的个数/bitset.cpp -------------------------------------------------------------------------------- /011-数值的整数次方/power_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/011-数值的整数次方/power_1.cpp -------------------------------------------------------------------------------- /011-数值的整数次方/power_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/011-数值的整数次方/power_2.cpp -------------------------------------------------------------------------------- /011-数值的整数次方/power_3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/011-数值的整数次方/power_3.cpp -------------------------------------------------------------------------------- /011-数值的整数次方/power_4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/011-数值的整数次方/power_4.cpp -------------------------------------------------------------------------------- /017-合并两个排序的链表/merge.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/017-合并两个排序的链表/merge.cpp -------------------------------------------------------------------------------- /019-二叉树的镜像/mirror.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/019-二叉树的镜像/mirror.jpg -------------------------------------------------------------------------------- /021-包含min函数的栈/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/021-包含min函数的栈/README.md -------------------------------------------------------------------------------- /021-包含min函数的栈/stack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/021-包含min函数的栈/stack.cpp -------------------------------------------------------------------------------- /023-从上往下打印二叉树/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/023-从上往下打印二叉树/README.md -------------------------------------------------------------------------------- /030-最小的K个数/01-sort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/030-最小的K个数/01-sort.cpp -------------------------------------------------------------------------------- /030-最小的K个数/02-heap.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/030-最小的K个数/02-heap.cpp -------------------------------------------------------------------------------- /034-丑数/02-uglynum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/034-丑数/02-uglynum.cpp -------------------------------------------------------------------------------- /041-和为S的连续正数序列/find.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/041-和为S的连续正数序列/find.cpp -------------------------------------------------------------------------------- /058-二叉树的下一个结点/bst.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/058-二叉树的下一个结点/bst.png -------------------------------------------------------------------------------- /064-数据流之中的中位数/multiset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/064-数据流之中的中位数/multiset -------------------------------------------------------------------------------- /004-替换空格/replacespace.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/004-替换空格/replacespace.cpp -------------------------------------------------------------------------------- /009-斐波那契数列/jumpFloor.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/009-斐波那契数列/jumpFloor.cpp -------------------------------------------------------------------------------- /009-斐波那契数列/rectCover.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/009-斐波那契数列/rectCover.cpp -------------------------------------------------------------------------------- /016-反转链表/reverselist1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/016-反转链表/reverselist1.cpp -------------------------------------------------------------------------------- /016-反转链表/reverselist2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/016-反转链表/reverselist2.cpp -------------------------------------------------------------------------------- /017-合并两个排序的链表/merge1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/017-合并两个排序的链表/merge1.cpp -------------------------------------------------------------------------------- /018-树的子结构/hassubtree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/018-树的子结构/hassubtree.cpp -------------------------------------------------------------------------------- /019-二叉树的镜像/mirror_in.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/019-二叉树的镜像/mirror_in.cpp -------------------------------------------------------------------------------- /019-二叉树的镜像/mirror_pre.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/019-二叉树的镜像/mirror_pre.cpp -------------------------------------------------------------------------------- /019-二叉树的镜像/mirror_re.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/019-二叉树的镜像/mirror_re.cpp -------------------------------------------------------------------------------- /024-二叉搜索树的后序遍历序列/tree.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/024-二叉搜索树的后序遍历序列/tree.jpg -------------------------------------------------------------------------------- /026-复杂链表的复制/linknext.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/026-复杂链表的复制/linknext.cpp -------------------------------------------------------------------------------- /031-连续子数组的最大和/03-sum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/031-连续子数组的最大和/03-sum.cpp -------------------------------------------------------------------------------- /031-连续子数组的最大和/04-sum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/031-连续子数组的最大和/04-sum.cpp -------------------------------------------------------------------------------- /033-把数组排成最小的数/minnum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/033-把数组排成最小的数/minnum.cpp -------------------------------------------------------------------------------- /034-丑数/01-bruteforce.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/034-丑数/01-bruteforce.cpp -------------------------------------------------------------------------------- /034-丑数/02-uglynum_set.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/034-丑数/02-uglynum_set.cpp -------------------------------------------------------------------------------- /039-二叉树的深度/treedepth.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/039-二叉树的深度/treedepth.cpp -------------------------------------------------------------------------------- /048-不能被继承的类/friend_class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/048-不能被继承的类/friend_class -------------------------------------------------------------------------------- /003-二维数组中的查找/bruteforce.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/003-二维数组中的查找/bruteforce.cpp -------------------------------------------------------------------------------- /005-从尾到头打印链表/print_stack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/005-从尾到头打印链表/print_stack.cpp -------------------------------------------------------------------------------- /007-用两个栈实现队列/stack2queue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/007-用两个栈实现队列/stack2queue.cpp -------------------------------------------------------------------------------- /007-用两个栈实现队列/stack2queue.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/007-用两个栈实现队列/stack2queue.jpg -------------------------------------------------------------------------------- /008-旋转数组的最小数字/minnumber.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/008-旋转数组的最小数字/minnumber.cpp -------------------------------------------------------------------------------- /009-斐波那契数列/fibonacci_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/009-斐波那契数列/fibonacci_1.cpp -------------------------------------------------------------------------------- /009-斐波那契数列/fibonacci_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/009-斐波那契数列/fibonacci_2.cpp -------------------------------------------------------------------------------- /009-斐波那契数列/jumpFloorII-2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/009-斐波那契数列/jumpFloorII-2.cpp -------------------------------------------------------------------------------- /009-斐波那契数列/jumpFloorII-3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/009-斐波那契数列/jumpFloorII-3.cpp -------------------------------------------------------------------------------- /009-斐波那契数列/jumpFloorII.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/009-斐波那契数列/jumpFloorII.cpp -------------------------------------------------------------------------------- /010-二进制中1的个数/numberof1_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/010-二进制中1的个数/numberof1_1.cpp -------------------------------------------------------------------------------- /010-二进制中1的个数/numberof1_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/010-二进制中1的个数/numberof1_2.cpp -------------------------------------------------------------------------------- /010-二进制中1的个数/numberof1_3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/010-二进制中1的个数/numberof1_3.cpp -------------------------------------------------------------------------------- /010-二进制中1的个数/numberof1_4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/010-二进制中1的个数/numberof1_4.cpp -------------------------------------------------------------------------------- /012-打印1到最大的N位数/print_string: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/012-打印1到最大的N位数/print_string -------------------------------------------------------------------------------- /015-链表中倒数第k个结点/findkth1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/015-链表中倒数第k个结点/findkth1.cpp -------------------------------------------------------------------------------- /015-链表中倒数第k个结点/findkth2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/015-链表中倒数第k个结点/findkth2.cpp -------------------------------------------------------------------------------- /019-二叉树的镜像/mirror_post.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/019-二叉树的镜像/mirror_post.cpp -------------------------------------------------------------------------------- /020-顺时针打印矩阵/printmartix.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/020-顺时针打印矩阵/printmartix.cpp -------------------------------------------------------------------------------- /020-顺时针打印矩阵/printmartix2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/020-顺时针打印矩阵/printmartix2.cpp -------------------------------------------------------------------------------- /022-栈的压入弹出序列/ispoporder1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/022-栈的压入弹出序列/ispoporder1.cpp -------------------------------------------------------------------------------- /022-栈的压入弹出序列/ispoporder2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/022-栈的压入弹出序列/ispoporder2.cpp -------------------------------------------------------------------------------- /022-栈的压入弹出序列/ispoporder3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/022-栈的压入弹出序列/ispoporder3.cpp -------------------------------------------------------------------------------- /023-从上往下打印二叉树/levelorder.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/023-从上往下打印二叉树/levelorder.cpp -------------------------------------------------------------------------------- /024-二叉搜索树的后序遍历序列/ispost_max: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/024-二叉搜索树的后序遍历序列/ispost_max -------------------------------------------------------------------------------- /026-复杂链表的复制/bruteforce.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/026-复杂链表的复制/bruteforce.cpp -------------------------------------------------------------------------------- /030-最小的K个数/03-partition.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/030-最小的K个数/03-partition.cpp -------------------------------------------------------------------------------- /030-最小的K个数/04-multiset.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/030-最小的K个数/04-multiset.cpp -------------------------------------------------------------------------------- /037-两个链表的第一个公共结点/03-map.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/037-两个链表的第一个公共结点/03-map.cpp -------------------------------------------------------------------------------- /009-斐波那契数列/fibonacci_logn.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/009-斐波那契数列/fibonacci_logn.cpp -------------------------------------------------------------------------------- /014-调整数组顺序使奇数位于偶数前面/buttle.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/014-调整数组顺序使奇数位于偶数前面/buttle.cpp -------------------------------------------------------------------------------- /024-二叉搜索树的后序遍历序列/ispost_n.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/024-二叉搜索树的后序遍历序列/ispost_n.cpp -------------------------------------------------------------------------------- /024-二叉搜索树的后序遍历序列/ispost_n2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/024-二叉搜索树的后序遍历序列/ispost_n2.cpp -------------------------------------------------------------------------------- /024-二叉搜索树的后序遍历序列/ispost_re.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/024-二叉搜索树的后序遍历序列/ispost_re.cpp -------------------------------------------------------------------------------- /027-二叉搜索树与双向链表/convert_re.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/027-二叉搜索树与双向链表/convert_re.cpp -------------------------------------------------------------------------------- /027-二叉搜索树与双向链表/convert_re2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/027-二叉搜索树与双向链表/convert_re2.cpp -------------------------------------------------------------------------------- /035-第一个只出现一次的字符位置/03-count.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/035-第一个只出现一次的字符位置/03-count.cpp -------------------------------------------------------------------------------- /046-求1+2+3+...+n/bitmulti.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/046-求1+2+3+...+n/bitmulti.cpp -------------------------------------------------------------------------------- /003-二维数组中的查找/divide-leftdown.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/003-二维数组中的查找/divide-leftdown.cpp -------------------------------------------------------------------------------- /003-二维数组中的查找/divide-rightup.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/003-二维数组中的查找/divide-rightup.cpp -------------------------------------------------------------------------------- /005-从尾到头打印链表/print_recursion.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/005-从尾到头打印链表/print_recursion.cpp -------------------------------------------------------------------------------- /005-从尾到头打印链表/print_recursion2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/005-从尾到头打印链表/print_recursion2.cpp -------------------------------------------------------------------------------- /008-旋转数组的最小数字/minnumber-On1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/008-旋转数组的最小数字/minnumber-On1.cpp -------------------------------------------------------------------------------- /008-旋转数组的最小数字/minnumber-On2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/008-旋转数组的最小数字/minnumber-On2.cpp -------------------------------------------------------------------------------- /012-打印1到最大的N位数/print_string.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/012-打印1到最大的N位数/print_string.cpp -------------------------------------------------------------------------------- /024-二叉搜索树的后序遍历序列/ispost_max.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/024-二叉搜索树的后序遍历序列/ispost_max.cpp -------------------------------------------------------------------------------- /027-二叉搜索树与双向链表/convert_stack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/027-二叉搜索树与双向链表/convert_stack.cpp -------------------------------------------------------------------------------- /028-字符串的排列/next_permutation.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/028-字符串的排列/next_permutation.cpp -------------------------------------------------------------------------------- /028-字符串的排列/permutation_error.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/028-字符串的排列/permutation_error.cpp -------------------------------------------------------------------------------- /029-数组中出现次数超过一半的数字/01-halfnum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/029-数组中出现次数超过一半的数字/01-halfnum.cpp -------------------------------------------------------------------------------- /031-连续子数组的最大和/01-bruteforce.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/031-连续子数组的最大和/01-bruteforce.cpp -------------------------------------------------------------------------------- /032-从1到n整数中1出现的次数/02-divide.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/032-从1到n整数中1出现的次数/02-divide.cpp -------------------------------------------------------------------------------- /032-从1到n整数中1出现的次数/02-divide2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/032-从1到n整数中1出现的次数/02-divide2.cpp -------------------------------------------------------------------------------- /035-第一个只出现一次的字符位置/02-bitmap.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/035-第一个只出现一次的字符位置/02-bitmap.cpp -------------------------------------------------------------------------------- /012-打印1到最大的N位数/print_recursion.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/012-打印1到最大的N位数/print_recursion.cpp -------------------------------------------------------------------------------- /012-打印1到最大的N位数/print_recursion2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/012-打印1到最大的N位数/print_recursion2.cpp -------------------------------------------------------------------------------- /014-调整数组顺序使奇数位于偶数前面/otherarray.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/014-调整数组顺序使奇数位于偶数前面/otherarray.cpp -------------------------------------------------------------------------------- /029-数组中出现次数超过一半的数字/02-partition.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/029-数组中出现次数超过一半的数字/02-partition.cpp -------------------------------------------------------------------------------- /029-数组中出现次数超过一半的数字/03-holdfield.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/029-数组中出现次数超过一半的数字/03-holdfield.cpp -------------------------------------------------------------------------------- /032-从1到n整数中1出现的次数/01-bruteforce.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/032-从1到n整数中1出现的次数/01-bruteforce.cpp -------------------------------------------------------------------------------- /037-两个链表的第一个公共结点/01-bruteforce.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/037-两个链表的第一个公共结点/01-bruteforce.cpp -------------------------------------------------------------------------------- /037-两个链表的第一个公共结点/02-align_stack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/037-两个链表的第一个公共结点/02-align_stack.cpp -------------------------------------------------------------------------------- /014-调整数组顺序使奇数位于偶数前面/reOrderArray.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/014-调整数组顺序使奇数位于偶数前面/reOrderArray.cpp -------------------------------------------------------------------------------- /025-二叉树中和为某一值的路径/findpath_left_arg.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/025-二叉树中和为某一值的路径/findpath_left_arg.cpp -------------------------------------------------------------------------------- /025-二叉树中和为某一值的路径/findpath_sum_arg.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/025-二叉树中和为某一值的路径/findpath_sum_arg.cpp -------------------------------------------------------------------------------- /037-两个链表的第一个公共结点/02-align_length.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/037-两个链表的第一个公共结点/02-align_length.cpp -------------------------------------------------------------------------------- /037-两个链表的第一个公共结点/02-align_strcat.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/037-两个链表的第一个公共结点/02-align_strcat.cpp -------------------------------------------------------------------------------- /061-按之字形顺序打印二叉树/levelorder_deque.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/061-按之字形顺序打印二叉树/levelorder_deque.cpp -------------------------------------------------------------------------------- /025-二叉树中和为某一值的路径/findpath_sum_static.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/025-二叉树中和为某一值的路径/findpath_sum_static.cpp -------------------------------------------------------------------------------- /025-二叉树中和为某一值的路径/findpath_left_static.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/025-二叉树中和为某一值的路径/findpath_left_static.cpp -------------------------------------------------------------------------------- /027-二叉搜索树与双向链表/convert_re_static_error.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/027-二叉搜索树与双向链表/convert_re_static_error.cpp -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/ArrayConstruction/ArrayConstruction.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/ChineseCodingInterviewAppendix-master/ArrayConstruction/ArrayConstruction.cpp -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/Utilities/Utilities.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/FirstCharacterInStream/FirstCharacterInStream.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/ChineseCodingInterviewAppendix-master/FirstCharacterInStream/FirstCharacterInStream.cpp -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/RegularExpressionsMatching/RegularExpressions.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awesome-interview/CodingInterviews/master/ChineseCodingInterviewAppendix-master/RegularExpressionsMatching/RegularExpressions.cpp -------------------------------------------------------------------------------- /046-求1+2+3+...+n/pointerofarray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int rich(int n) 5 | { 6 | return ( (int)( &((uint8_t (*) [n])0)[1+n][0]) ) >> 1; 7 | } 8 | 9 | int main() { 10 | printf("%d\n", rich(10)); 11 | 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /053-正则表达式匹配/re.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | 6 | import re 7 | 8 | class Solution: 9 | # @return a boolean 10 | def isMatch(self, s, p): 11 | return re.match('^' + p + '$', s) != None 12 | 13 | # debug 14 | s = Solution() 15 | print s.isMatch("aa", "a*") -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/README.md: -------------------------------------------------------------------------------- 1 | ChineseCodingInterviewAppendix 2 | ============================== 3 | 4 | [zhedahht/ChineseCodingInterviewAppendix](https://github.com/zhedahht/ChineseCodingInterviewAppendix) 5 | [微软、Google等面试题](http://zhedahht.blog.163.com/) 6 | 7 | The source code for the appendix part of the Chinese version of the book Coding Interviews 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | -------------------------------------------------------------------------------- /template/template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | 21 | 22 | int __tmain( ) 23 | { 24 | debug <<"test" < 2 | #include 3 | 4 | 5 | 6 | // 调试开关 7 | #define DEBUG 8 | 9 | 10 | #ifdef DEBUG 11 | 12 | #define debug cout 13 | #define dprintf printf 14 | 15 | #else 16 | 17 | #define debug 0 && cout 18 | #define dprintf 0 && printf 19 | 20 | #endif // DEBUG 21 | 22 | 23 | 24 | 25 | int main( ) 26 | { 27 | freopen("in.txt", "r", stdin); 28 | //freopen("out.txt", "w", stdout); 29 | 30 | int a, b; 31 | while(scanf("%d%d", &a, &b) != EOF) 32 | { 33 | printf("%d\n", a + b); 34 | } 35 | 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/Utilities/BinaryTree.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | struct BinaryTreeNode 4 | { 5 | int m_nValue; 6 | BinaryTreeNode* m_pLeft; 7 | BinaryTreeNode* m_pRight; 8 | }; 9 | 10 | __declspec( dllexport ) BinaryTreeNode* CreateBinaryTreeNode(int value); 11 | __declspec( dllexport ) void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight); 12 | __declspec( dllexport ) void PrintTreeNode(BinaryTreeNode* pNode); 13 | __declspec( dllexport ) void PrintTree(BinaryTreeNode* pRoot); 14 | __declspec( dllexport ) void DestroyTree(BinaryTreeNode* pRoot); 15 | -------------------------------------------------------------------------------- /046-求1+2+3+...+n/functemplate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | template < unsigned int n> 21 | struct Sum 22 | { 23 | enum Value 24 | { 25 | N = Sum< n - 1 >::N + n 26 | }; 27 | }; 28 | 29 | template <> 30 | struct Sum<1> 31 | { 32 | enum Value 33 | { 34 | N = 1 35 | }; 36 | }; 37 | 38 | 39 | int main( ) 40 | { 41 | 42 | cout <::N < 2 | #include 3 | 4 | using namespace std; 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | 21 | class Solutin 22 | { 23 | public : 24 | void Print(int n) 25 | { 26 | int max = pow(10, n); 27 | for(int i = 1; i < max; i++) 28 | { 29 | cout <> n) 42 | { 43 | 44 | solu.Print(n); 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /048-不能被继承的类/static_func.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | class SealedClass 21 | { 22 | public : 23 | static SealedClass* GetInstance( ) 24 | { 25 | return new SealedClass( ); 26 | } 27 | private : 28 | SealedClass( ){ } 29 | ~SealedClass( ){ }; 30 | 31 | }; 32 | 33 | class Base : public SealedClass 34 | { 35 | }; 36 | 37 | int __tmain( ) 38 | { 39 | SealedClass *pb = Base::GetInstance( ); 40 | Base base; 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /template/oj.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | 7 | using namespace std; 8 | 9 | 10 | 11 | // 调试开关 12 | #define DEBUG 13 | 14 | 15 | #ifdef DEBUG 16 | 17 | #define debug cout 18 | #define dprintf printf 19 | 20 | #else 21 | 22 | #define debug 0 && cout 23 | #define dprintf 0 && printf 24 | 25 | #endif // DEBUG 26 | 27 | 28 | 29 | 30 | int main( ) 31 | { 32 | freopen("in.txt", "r", stdin); 33 | //freopen("out.txt", "w", stdout); 34 | 35 | int a, b; 36 | while(scanf("%d%d", &a, &b) != EOF) 37 | { 38 | printf("%d\n", a + b); 39 | } 40 | 41 | 42 | return EXIT_SUCCESS; 43 | } 44 | -------------------------------------------------------------------------------- /047-不用加减乘除做加法/inline_asm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | class Solution 21 | { 22 | public: 23 | int Add(int left, int right) 24 | { 25 | __asm__ __volatile__ 26 | ( 27 | //"lock;\n" 28 | "addl %1,%0;\n" /* 相当于 add b, a结果存储在a中*/ 29 | : "=m"(left) 30 | : "r"(right), "m"(left) 31 | // : 32 | ); 33 | 34 | return left; 35 | } 36 | }; 37 | 38 | int __tmain( ) 39 | { 40 | Solution solu; 41 | 42 | 43 | cout < 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | class SealedClass 21 | { 22 | private : 23 | SealedClass( ){ } 24 | ~SealedClass( ){ } 25 | 26 | public : 27 | friend SealedClass* GetInstance( ); 28 | 29 | }; 30 | 31 | 32 | SealedClass* GetInstance( ) 33 | { 34 | return new SealedClass( ); 35 | } 36 | 37 | 38 | class Base : public SealedClass 39 | { 40 | }; 41 | 42 | int __tmain( ) 43 | { 44 | SealedClass *p = GetInstance( ); 45 | 46 | //Base base; // error 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /046-求1+2+3+...+n/recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | class Solution 20 | { 21 | public: 22 | int Sum_Solution(int n) 23 | { 24 | return 0; 25 | } 26 | 27 | int SumRecursion(int n) 28 | { 29 | int ans = n; 30 | debug < 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | 8 | 9 | 10 | #define __tmain main 11 | 12 | #ifdef __tmain 13 | 14 | #define debug cout 15 | 16 | #else 17 | 18 | #define debug 0 && cout 19 | 20 | #endif // __tmain 21 | 22 | 23 | class Solution 24 | { 25 | public: 26 | int GetNumberOfK(vector data ,int k) 27 | { 28 | int upper = upper_bound(data.begin(),data.end(),k); 29 | int low = lower_bound(data.begin(),data.end(),k); 30 | return upper - low; 31 | } 32 | }; 33 | 34 | int __tmain( ) 35 | { 36 | Solution solu; 37 | 38 | int arr[] = { 1, 2, 3, 3, 3, 3, 4, 5 }; 39 | vector vec(arr, arr + 8); 40 | cout < 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | 21 | class CNoHeritance 22 | { 23 | friend class SealedClass; 24 | private: 25 | CNoHeritance(){ } 26 | ~CNoHeritance(){ } 27 | }; 28 | 29 | 30 | 31 | class SealedClass : virtual public CNoHeritance 32 | { 33 | public: 34 | SealedClass( ){ } 35 | 36 | ~SealedClass( ){ } 37 | }; 38 | 39 | 40 | /* 41 | class Base : public SealedClass 42 | { 43 | public: 44 | Base():SealedClass( ){ } 45 | ~Base(){ } 46 | }; 47 | */ 48 | 49 | int __tmain( ) 50 | { 51 | SealedClass sc; 52 | //Base base; 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /048-不能被继承的类/final.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | 21 | template 22 | class CNoHeritance 23 | { 24 | friend T; 25 | private: 26 | CNoHeritance(){ } 27 | ~CNoHeritance(){ } 28 | }; 29 | 30 | 31 | 32 | class SealedClass : virtual public CNoHeritance 33 | { 34 | public: 35 | SealedClass( ){ } 36 | 37 | ~SealedClass( ){ } 38 | }; 39 | 40 | /* 41 | class Base : public SealedClass 42 | { 43 | public: 44 | Base():SealedClass( ){ } 45 | ~Base(){ } 46 | }; 47 | */ 48 | 49 | int __tmain( ) 50 | { 51 | SealedClass sc; 52 | //Base base; 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /038-数字在排序数组中出现的次数/04-multiset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | 8 | 9 | 10 | // 调试开关 11 | #define __tmain main 12 | 13 | #ifdef __tmain 14 | 15 | #define debug cout 16 | 17 | #else 18 | 19 | #define debug 0 && cout 20 | 21 | #endif // __tmain 22 | 23 | 24 | class Solution 25 | { 26 | public: 27 | int GetNumberOfK(vector data ,int k) 28 | { 29 | multiset mData; 30 | for(int i = 0; i < data.size( ); i++) 31 | { 32 | mData.insert(data[i]); 33 | } 34 | 35 | return mData.count(k); 36 | } 37 | }; 38 | 39 | int __tmain( ) 40 | { 41 | Solution solu; 42 | 43 | int arr[] = { 1, 2, 3, 3, 3, 3, 4, 5 }; 44 | vector vec(arr, arr + 8); 45 | cout < 2 | 3 | using namespace std; 4 | 5 | // 调试开关 6 | #define __tmain main 7 | 8 | #ifdef __tmain 9 | 10 | #define debug cout 11 | 12 | #else 13 | 14 | #define debug 0 && cout 15 | 16 | #endif // __tmain 17 | 18 | 19 | class Solution 20 | { 21 | public: 22 | string LeftRotateString(string str, int n) 23 | { 24 | /// abcXYZdef 25 | /// abcXYZdefabcXYZdef 26 | /// 012345678 27 | /// 新的串是两倍串第len位起始长度为n字串 28 | int length = str.size( ); 29 | 30 | if(length == 0) 31 | { 32 | return ""; 33 | } 34 | n = n % len; 35 | str += str; 36 | 37 | return str.substr(n, len); 38 | } 39 | }; 40 | 41 | 42 | int __tmain( ) 43 | { 44 | Solution solu; 45 | 46 | cout < 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | class Solution 21 | { 22 | public: 23 | int Add(int left, int right) 24 | { 25 | int temp; 26 | while(right != 0) 27 | { 28 | temp = left ^ right; // 计算不带进位的情况 29 | right = (left & right) <<1; // 计算带进位的情况 30 | left = temp; 31 | // now left = 不带进位的情况, right = 带进位的情况 32 | } 33 | 34 | return left; 35 | } 36 | 37 | 38 | int AddRecursion(int left, int right) 39 | { 40 | return right == 0 ? left : AddRecursion(left ^ right, (left & right) << 1); 41 | } 42 | }; 43 | 44 | int __tmain( ) 45 | { 46 | Solution solu; 47 | 48 | 49 | cout < 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | // 调试开关 8 | #define __tmain main 9 | 10 | #ifdef __tmain 11 | 12 | #define debug cout 13 | 14 | #else 15 | 16 | #define debug 0 && cout 17 | 18 | #endif // __tmain 19 | 20 | 21 | class Solution 22 | { 23 | public: 24 | vector multiply(const vector& A) 25 | { 26 | int n = A.size(); 27 | vector res(n); 28 | // C[i] = A[0] * A[1] * ... *A[i - 1] 29 | for(int i = 0, temp = 1; i < n; i++) 30 | { 31 | res[i] = temp; 32 | temp *= A[i]; 33 | } 34 | 35 | // D[i] = A[i + 1] * A[i + 2] * ... *A[n - 1] 36 | for(int i = n - 1, temp = 1; i >= 0; i--) 37 | { 38 | res[i] *= temp; 39 | temp *= A[i]; 40 | 41 | } 42 | return res; 43 | } 44 | }; 45 | 46 | int __tmain( ) 47 | { 48 | debug <<"test" < 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/KthNodeInBST/KthNodeInBST.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/StreamMedian/StreamMedian.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /042-翻转单词顺序列/04-reverse_temp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // 调试开关 6 | #define __tmain main 7 | 8 | #ifdef __tmain 9 | 10 | #define debug cout 11 | 12 | #else 13 | 14 | #define debug 0 && cout 15 | 16 | #endif // __tmain 17 | 18 | 19 | class Solution 20 | { 21 | public: 22 | string ReverseSentence(string str) 23 | { 24 | string result; 25 | while (str.rfind(" ") != -1) 26 | { 27 | unsigned long position = str.rfind(" "); 28 | string temp = str.substr(position + 1); 29 | //debug < 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/StringPathInMatrix/StringPathInMatrix.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/ArrayConstruction/ArrayConstruction.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/DuplicationInArray/DuplicationInArray.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/NextNodeInBinaryTrees/NextNodeInBinaryTrees.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/PrintTreesInLines/PrintTreesInLines.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/EntryNodeInListLoop/EntryNodeInListLoop.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/MaxInSlidingWindow/MaxInSlidingWindow.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/PrintTreesInZigzag/PrintTreesInZigzag.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/SerializeBinaryTrees/SerializeBinaryTrees.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/DeleteDuplicatedListNode/DeleteDuplicatedListNode.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/FirstCharacterInStream/FirstCharacterInStream.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/SymmetricalBinaryTree/SymmetricalBinaryTree.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /038-数字在排序数组中出现的次数/01-bruteforce.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | using namespace std; 6 | 7 | 8 | 9 | // 调试开关 10 | #define __tmain main 11 | 12 | #ifdef __tmain 13 | 14 | #define debug cout 15 | 16 | #else 17 | 18 | #define debug 0 && cout 19 | 20 | #endif // __tmain 21 | 22 | 23 | class Solution 24 | { 25 | 26 | public: 27 | 28 | int GetNumberOfK(vector data,int k) 29 | { 30 | if(data.size( ) == 0) 31 | { 32 | return 0; 33 | } 34 | 35 | int count = 0; 36 | for(unsigned int i = 0; 37 | i < data.size( ) && data[i] <= k; 38 | i++) 39 | { 40 | if(data[i] == k) 41 | { 42 | count++; 43 | } 44 | } 45 | 46 | return count; 47 | } 48 | }; 49 | 50 | int __tmain( ) 51 | { 52 | Solution solu; 53 | 54 | int arr[] = { 1, 2, 3, 3, 3, 3, 5, 6 }; 55 | vector vec(arr, arr + 8); 56 | cout < 2 | 3 | using namespace std; 4 | 5 | // 调试开关 6 | #define __tmain main 7 | 8 | #ifdef __tmain 9 | 10 | #define debug cout 11 | 12 | #else 13 | 14 | #define debug 0 && cout 15 | 16 | #endif // __tmain 17 | 18 | class Solution 19 | { 20 | public: 21 | string LeftRotateString(string str, int n) 22 | { 23 | if(str.size( ) == 0) 24 | { 25 | return ""; 26 | } 27 | n %= str.size( ); 28 | 29 | Reverse(str, 0, n - 1); 30 | Reverse(str, n, str.size( ) - 1); 31 | Reverse(str, 0, str.size( ) - 1); 32 | 33 | return str; 34 | } 35 | 36 | void Reverse(string &str, int left, int right) 37 | { 38 | while(left < right) 39 | { 40 | swap(str[left], str[right]); 41 | 42 | left++; 43 | right--; 44 | } 45 | } 46 | }; 47 | 48 | 49 | int __tmain( ) 50 | { 51 | Solution solu; 52 | 53 | cout < 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /036-数组中的逆序对/01-bruteforce.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | #define __tmain main 7 | 8 | #ifdef __tmain 9 | 10 | #define debug cout 11 | 12 | #else 13 | 14 | #define debug 0 && cout 15 | 16 | #endif // __tmain 17 | 18 | 19 | class Solution 20 | { 21 | public: 22 | int InversePairs(vector elem) 23 | { 24 | if(elem.size( ) == 0) 25 | { 26 | return 0; 27 | } 28 | 29 | int count = 0; 30 | for(unsigned int i = 0; i < elem.size( ); i++) /// 循环每一个位置的数据 31 | { 32 | for(unsigned int j = i + 1; j < elem.size( ); j++) /// 依次判断后面的数据与当前位置数据是否是逆序 33 | { 34 | if(elem[i] > elem[j]) 35 | { 36 | count++; 37 | } 38 | } 39 | } 40 | 41 | return count; 42 | } 43 | }; 44 | 45 | int __tmain( ) 46 | { 47 | Solution solu; 48 | 49 | int arr[] = { 7, 5, 6, 4 }; 50 | vector vec(arr, arr + 4); 51 | 52 | cout < 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | class SealedClass 21 | { 22 | public : 23 | static SealedClass* GetInstance( ) 24 | { 25 | if(m_sc == NULL) 26 | { 27 | m_sc = new SealedClass( ); 28 | } 29 | 30 | return m_sc; 31 | } 32 | static void Destroy( ) 33 | { 34 | if(m_sc != NULL) 35 | { 36 | delete m_sc; 37 | } 38 | } 39 | private : 40 | SealedClass( ){ } 41 | ~SealedClass( ){ } 42 | 43 | 44 | static SealedClass *m_sc; 45 | }; 46 | 47 | 48 | SealedClass* SealedClass::m_sc = NULL; 49 | 50 | 51 | class Base : public SealedClass 52 | { 53 | }; 54 | 55 | 56 | int __tmain( ) 57 | { 58 | SealedClass *pb = SealedClass::GetInstance( ); 59 | SealedClass::Destroy( ); 60 | 61 | pb = NULL; 62 | 63 | //Base b; // error 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /004-替换空格/replacespace2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define __tmain main 6 | 7 | 8 | class Solution 9 | { 10 | public: 11 | void replaceSpace(char *str,int length) 12 | { 13 | int i, j; 14 | int count = 0; 15 | int len = length; 16 | for(int i = 0; i < length; i++) 17 | { 18 | if(str[i] == ' ') 19 | { 20 | count++; 21 | } 22 | } 23 | 24 | len = length + count * 2; 25 | for(i = length - 1, j = len - 1; 26 | i >= 0 && j >= 0;) 27 | { 28 | if(str[i] == ' ') 29 | { 30 | str[j--] = '0'; 31 | str[j--] = '2'; 32 | str[j--] = '%'; 33 | i--; 34 | } 35 | else 36 | { 37 | str[j--] = str[i--]; 38 | } 39 | } 40 | str[len] = '0'; 41 | 42 | } 43 | }; 44 | 45 | 46 | int __tmain( ) 47 | { 48 | char str[10 + 1] = "a b c d"; 49 | 50 | Solution solu; 51 | solu.replaceSpace(str, 10); 52 | 53 | cout < 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | class base; 21 | base *parray[2]; 22 | 23 | class base{ 24 | public: 25 | virtual unsigned int sum(unsigned int n) 26 | { 27 | // 递归的终止 28 | return 0; 29 | } 30 | }; 31 | 32 | class derive :public base{ 33 | public: 34 | virtual unsigned int sum(unsigned int n) 35 | { 36 | // !!n两次非运算, 从而将n转换为bool类型, 来选择函数 37 | // sum(n) = sum(n - 1) + n 则是递归公式 38 | return parray[!!n]->sum(n - 1) + n; 39 | } 40 | }; 41 | 42 | 43 | class Solution { 44 | public: 45 | int Sum_Solution(int n) { 46 | 47 | base a; 48 | derive b; 49 | 50 | parray[0] = &a; 51 | parray[1] = &b; 52 | 53 | return parray[1]->sum(n); 54 | } 55 | }; 56 | 57 | 58 | int main(){ 59 | 60 | Solution s; 61 | 62 | cout< 2 | 3 | using namespace std; 4 | 5 | // 调试开关 6 | #define __tmain main 7 | 8 | #ifdef __tmain 9 | 10 | #define debug cout 11 | 12 | #else 13 | 14 | #define debug 0 && cout 15 | 16 | #endif // __tmain 17 | 18 | 19 | class Solution 20 | { 21 | public: 22 | string ReverseSentence(string str) 23 | { 24 | if(str.size( ) == 0) 25 | { 26 | return str; 27 | } 28 | string res = "", tmp = ""; 29 | for(unsigned int i = 0; i < str.size(); ++i) 30 | { 31 | if(str[i] == ' ') // 发现一个单词 32 | { 33 | debug < 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | class base; 21 | base *parray[2]; 22 | 23 | class base{ 24 | public: 25 | virtual unsigned int sum(unsigned int n) 26 | { 27 | // 递归的终止 28 | return 0; 29 | } 30 | }; 31 | 32 | class derive :public base{ 33 | public: 34 | virtual unsigned int sum(unsigned int n) 35 | { 36 | // !!n两次非运算, 从而将n转换为bool类型, 来选择函数 37 | // sum(n) = sum(n - 1) + n 则是递归公式 38 | return parray[!!n]->sum(n - 1) + n; 39 | } 40 | }; 41 | 42 | 43 | class Solution 44 | { 45 | public: 46 | int Sum_Solution(int n) 47 | { 48 | 49 | base a; 50 | derive b; 51 | 52 | parray[0] = &a; 53 | parray[1] = &b; 54 | 55 | return parray[1]->sum(n); 56 | } 57 | }; 58 | 59 | 60 | int main(){ 61 | 62 | Solution s; 63 | 64 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | class base; 21 | base *parray[2]; 22 | 23 | class base{ 24 | public: 25 | virtual unsigned int sum(unsigned int n) 26 | { 27 | // 递归的终止 28 | return 0; 29 | } 30 | }; 31 | 32 | class derive :public base{ 33 | public: 34 | virtual unsigned int sum(unsigned int n) 35 | { 36 | // !!n两次非运算, 从而将n转换为bool类型, 来选择函数 37 | // sum(n) = sum(n - 1) + n 则是递归公式 38 | return parray[!!n]->sum(n - 1) + n; 39 | } 40 | }; 41 | 42 | 43 | class Solution 44 | { 45 | public: 46 | int Sum_Solution(int n) 47 | { 48 | 49 | base a; 50 | derive b; 51 | 52 | parray[0] = &a; 53 | parray[1] = &b; 54 | 55 | return parray[1]->sum(n); 56 | } 57 | }; 58 | 59 | 60 | int main(){ 61 | 62 | Solution s; 63 | 64 | cout< 2 | 3 | 4 | using namespace std; 5 | 6 | 7 | // 调试开关 8 | #define __tmain main 9 | 10 | #ifdef __tmain 11 | 12 | #define debug cout 13 | 14 | #else 15 | 16 | #define debug 0 && cout 17 | 18 | #endif // __tmain 19 | 20 | 21 | 22 | class Solution 23 | { 24 | public: 25 | string LeftRotateString(string str, int n) 26 | { 27 | if(str.size( ) == 0) 28 | { 29 | return ""; 30 | } 31 | n %= str.size( ); 32 | Reverse(str.begin( ), str.begin( ) + n - 1); 33 | Reverse(str.begin( ) + n, str.end( ) - 1); 34 | Reverse(str.begin( ), str.end( ) - 1); 35 | 36 | return str; 37 | } 38 | 39 | void Reverse(string::iterator left, string::iterator right) 40 | { 41 | while(left < right) 42 | { 43 | debug <<*left <<", " <<*right < 2 | #include 3 | using namespace std; 4 | 5 | 6 | 7 | ///if p[j+1] == '*' -> (i + 1, j + 1) 8 | ///else if p[i] == p[j] -> (i + 1, j + 2) or (i, j+2) 9 | ///else -> (i, j+2) 10 | 11 | class Solution 12 | { 13 | public: 14 | bool match(const char *s, const char *p) 15 | { 16 | if (*p == '\0') // 正则p到底末尾时 17 | { 18 | return !(*s); // 如果串s页到达末尾,则匹配成功 19 | } 20 | 21 | int slen = strlen(s), plen = strlen(p); 22 | 23 | if (plen == 1 // 如果正则串只有一个长度 24 | || *(p + 1) != '*') // 如果匹配* 25 | { 26 | // 27 | return slen && (p[0] == '.' || *s == *p) 28 | && match(s + 1, p + 1); 29 | } 30 | else 31 | { 32 | // 匹配一个字符 33 | while (*s != '\0' && (*p == '.' || *s == *p)) 34 | { 35 | if (match(s++, p + 2)) 36 | { 37 | return true; 38 | } 39 | } 40 | } 41 | 42 | // 匹配0个字符 43 | return match(s, p + 2); 44 | } 45 | }; 46 | 47 | 48 | 49 | int main(void) 50 | { 51 | 52 | } 53 | -------------------------------------------------------------------------------- /046-求1+2+3+...+n/constructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | class Temp 20 | { 21 | public : 22 | Temp( ) 23 | { 24 | ++N; 25 | Sum += N; 26 | } 27 | 28 | static void Reset( ) 29 | { 30 | N = 0; 31 | Sum = 0; 32 | } 33 | 34 | static unsigned int GetSum( ) 35 | { 36 | return Sum; 37 | } 38 | 39 | private : 40 | static unsigned int N; 41 | static unsigned int Sum; 42 | }; 43 | 44 | unsigned int Temp::N = 0; 45 | unsigned int Temp::Sum = 0; 46 | 47 | 48 | 49 | class Solution 50 | { 51 | public: 52 | int res; 53 | int Sum_Solution(int n) 54 | { 55 | Temp::Reset( ); 56 | 57 | Temp *a = new Temp[n]; 58 | delete[] a; 59 | a = NULL; 60 | 61 | return Temp::GetSum( ); 62 | } 63 | }; 64 | 65 | // 5 4 3 2 1 66 | int __tmain(void) 67 | { 68 | Solution solu; 69 | cout < 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // 调试开关 8 | #define __tmain main 9 | 10 | #ifdef __tmain 11 | 12 | #define debug cout 13 | 14 | #else 15 | 16 | #define debug 0 && cout 17 | 18 | #endif // __tmain 19 | 20 | 21 | class Solution 22 | { 23 | public: 24 | int LastRemaining_Solution(unsigned int n, unsigned int m) 25 | { 26 | if(n < 1 || m < 1) 27 | { 28 | return -1; 29 | } 30 | else if(n == 1) 31 | { 32 | return 0; 33 | } 34 | else 35 | { 36 | // F[n] = (F[n - 1] + m) % n 37 | return (LastRemaining_Solution(n - 1, m) + m) % n; 38 | } 39 | } 40 | }; 41 | 42 | int __tmain( ) 43 | { 44 | Solution solu; 45 | /* 46 | 0 1 2 3 4 -=> 1 leave, last = 0, m = 2, step = 2, 47 | 0 2 3 4 -=> 3 leave, last = 0, m = 2, step = 3, 48 | 0 2 4 -=> 0 leave, last = 2, m = 2, step = 4, 49 | 2 4 -=> 4 leave 50 | 2 -=> 2 win 51 | */ 52 | cout < 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // µ÷ÊÔ¿ª¹Ø 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | class Solution 20 | { 21 | public: 22 | //下面这种迭代的方法时间复杂度为O(n^2) 23 | bool VerifySquenceOfBST_Iteratively(const vector& sequence) { 24 | int left = 0, right = sequence.size() - 1; 25 | while (left < right && right != 0) { 26 | //循环结束时,left是第一个大于根的元素的位置,即右子树首元素下标 27 | while (sequence[left] < sequence[right]) 28 | left++; 29 | //循环结束时,left越过右子树末节点,到达根节点 30 | while (sequence[left] > sequence[right]) 31 | left++; 32 | 33 | //如果没有到达根节点,说明不满足左子树所有节点小于根节点,右子树所有节点大于根节点的条件 34 | if (left < right) 35 | return false; 36 | left = 0; 37 | 38 | right--; 39 | } 40 | 41 | //如果循环一直到right == 0才终止, 说明以所有节点为根节点,左侧序列均满足后序遍历序列的条件 42 | return (right == 0) ? true : false; 43 | } 44 | }; 45 | 46 | 47 | int __tmain( ) 48 | { 49 | // 10 50 | // 6 14 51 | // 8 12 16 52 | //4 53 | 54 | 55 | 56 | 57 | int a[] = { 1, 2, 3 }; 58 | 59 | vector vec(a, a + 3); 60 | 61 | Solution solu; 62 | cout < 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | 26 | 27 | Header Files 28 | 29 | 30 | Header Files 31 | 32 | 33 | -------------------------------------------------------------------------------- /041-和为S的两个数字/find.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | using namespace std; 6 | 7 | 8 | // 调试开关 9 | #define __tmain main 10 | 11 | #ifdef __tmain 12 | 13 | #define debug cout 14 | 15 | #else 16 | 17 | #define debug 0 && cout 18 | 19 | #endif // __tmain 20 | 21 | 22 | class Solution 23 | { 24 | 25 | public : 26 | 27 | vector FindNumbersWithSum(vector array,int sum) 28 | { 29 | vector res; 30 | 31 | if (array.size( ) < 2) 32 | { 33 | return res; 34 | } 35 | 36 | int start = 0, end = array.size( ) - 1; 37 | long curSum; 38 | 39 | while (start < end) 40 | { 41 | curSum = array[start] + array[end]; 42 | 43 | if (curSum == sum) 44 | { 45 | /// 左右夹逼 46 | /// 同时能保证乘积最小的 47 | /// 和为sum的最大的两个数最接近sqrt(sum) 48 | res.push_back(array[start]); 49 | res.push_back(array[end]); 50 | 51 | break; 52 | } 53 | else if (curSum < sum) 54 | { 55 | start++; 56 | } 57 | else 58 | { 59 | end--; 60 | } 61 | } 62 | 63 | return res; 64 | } 65 | }; 66 | 67 | int __tmain( ) 68 | { 69 | debug <<"test" < 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | 8 | 9 | 10 | #define __tmain main 11 | 12 | #ifdef __tmain 13 | 14 | #define debug cout 15 | 16 | #else 17 | 18 | #define debug 0 && cout 19 | 20 | #endif // __tmain 21 | 22 | /////////////////////////////////// 23 | // error 24 | /////////////////////////////////// 25 | class Solution 26 | { 27 | public: 28 | //从data末端开始,对每个数进行插入排序 其插入入的位置与其当前位置之差 即为当前数字的逆序个数 29 | int InversePairs(vector elem) 30 | { 31 | // 排序部分 32 | int i, j, temp; 33 | int count = 0, length = elem.size( ); 34 | 35 | for(i = 1; i < length; i++) // 循环第1个数据以后的每个数据 36 | { 37 | temp = elem[i]; // 保存第i个数据, 38 | for(j = i - 1; j >= 0 && temp < elem[j]; j--) // 将比保存的数据都往后移 39 | { 40 | elem[j + 1] = elem[j]; 41 | count++; 42 | } 43 | // 循环结束后就在有序的0-i的位置中找到了temp应该插入的位置(j + 1) 44 | elem[j + 1] = temp; // 将保存的数据插进来 45 | } 46 | 47 | copy(elem.begin(), elem.end(), ostream_iterator(cout," ")); 48 | return count; 49 | } 50 | }; 51 | 52 | int __tmain( ) 53 | { 54 | Solution solu; 55 | 56 | int arr[] = { 7, 6, 5, 4}; 57 | vector vec(arr, arr + 4); 58 | 59 | cout < 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | class Solution 20 | { 21 | public: 22 | int MoreThanHalfNum_Solution(vector numbers) 23 | { 24 | // return numbers.empty( ) ? 0 : *find_if(numbers.begin( ), numbers.end( ),[numbers](int x) 25 | // { 26 | // return (count(numbers.begin( ),numbers.end( ), x) * 2) > numbers.size(); 27 | // }); 28 | 29 | if(numbers.size( ) == 0) 30 | { 31 | return 0; 32 | } 33 | for(int i = 0 ; i < numbers.size( ); i++) 34 | { 35 | debug <<"num = " < 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | using namespace std; 8 | 9 | 10 | // 调试开关 11 | #define __tmain main 12 | 13 | #ifdef __tmain 14 | 15 | #define debug cout 16 | 17 | #else 18 | 19 | #define debug 0 && cout 20 | 21 | #endif // __tmain 22 | 23 | class Solution 24 | { 25 | typedef pair Pair; 26 | public : 27 | vector maxInWindows(const vector &num, unsigned int size) 28 | { 29 | vector result; 30 | priority_queue Q; 31 | if (num.size() < size || size < 1) 32 | { 33 | return result; 34 | } 35 | 36 | for (int i = 0; i < size-1; i++) 37 | { 38 | Q.push(Pair(num[i],i)); 39 | } 40 | 41 | for (int i = size-1; i < num.size(); i++) 42 | { 43 | Q.push(Pair(num[i],i)); 44 | Pair p = Q.top(); 45 | while(p.second < i-(size-1)) { 46 | Q.pop(); 47 | p = Q.top(); 48 | } 49 | result.push_back(p.first); 50 | } 51 | // result.push_back(Q.top().first); 52 | return result; 53 | } 54 | }; 55 | int __tmain( ) 56 | { 57 | Solution solu; 58 | 59 | int array[] = { 2, 3, 4, 2, 6, 2, 5, 1 }; 60 | vector vec(array, array + 8); 61 | 62 | vector res = solu.maxInWindows(vec, 3); 63 | copy(res.begin( ), res.end( ), ostream_iterator(cout," ")); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /029-数组中出现次数超过一半的数字/05-map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | #define __tmain main 9 | 10 | #ifdef __tmain 11 | 12 | #define debug cout 13 | 14 | #else 15 | 16 | #define debug 0 && cout 17 | 18 | #endif // __tmain 19 | 20 | class Solution 21 | { 22 | public: 23 | int MoreThanHalfNum_Solution(vector numbers) 24 | { 25 | map hmap; // 建立数字到出现次数的映射 26 | 27 | int nsize = numbers.size( ); 28 | 29 | for(int i = 0; i < nsize; i++) 30 | { 31 | if(hmap.find(numbers[i]) != hmap.end( )) 32 | { 33 | hmap[numbers[i]] += 1; 34 | } 35 | else 36 | { 37 | hmap[numbers[i]] = 1; 38 | } 39 | } 40 | 41 | // 查找到出现次数最多的那个元素 42 | for(map::iterator it = hmap.begin( ); 43 | it != hmap.end( ); 44 | ++it) 45 | { 46 | debug <first <<", " <second <second * 2 > nsize) 48 | { 49 | return it->first; 50 | } 51 | } 52 | return 0; 53 | } 54 | }; 55 | 56 | 57 | int __tmain( ) 58 | { 59 | int arr1[] = { 1,2,3,2,2,2,5,4,2}; 60 | 61 | vector vec1(arr1, arr1 + 9); 62 | 63 | Solution solu; 64 | cout < vec2(arr2, arr2 + 2); 70 | cout < 2 | #include 3 | 4 | using namespace std; 5 | 6 | #define __tmain main 7 | 8 | #ifdef __tmain 9 | 10 | #define debug cout 11 | 12 | #else 13 | 14 | #define debug 0 && cout 15 | 16 | #endif // __tmain 17 | 18 | 19 | class Solution 20 | { 21 | public: 22 | int InversePairs(vector elem) 23 | { 24 | if(elem.size( ) == 0) 25 | { 26 | return 0; 27 | } 28 | 29 | return BubbleSort(elem); 30 | } 31 | 32 | int BubbleSort(vector elem) 33 | { 34 | bool flag = true; 35 | int count = 0; 36 | unsigned int length = elem.size( ); 37 | 38 | for(unsigned int i = 0; 39 | i < length - 1 && flag == true; 40 | i++) // 共计进行length-1趟循环 41 | { 42 | flag = false; // 初始默认数据是无序的 43 | for(unsigned int j = 0; 44 | j < length - i - 1; 45 | j++) // 每趟循环比较length-1-j次 46 | { 47 | if(elem[j] > elem[j + 1]) // 如果当前的元素与后一个元素不满足排序规则 48 | { 49 | swap(elem[j], elem[j+1]); 50 | flag = true; // 仍然需要交换说明, 仍然无序 51 | count++; 52 | } 53 | } 54 | } 55 | 56 | return count; 57 | } 58 | }; 59 | 60 | int __tmain( ) 61 | { 62 | Solution solu; 63 | 64 | int arr[] = { 1,2,3,4,7,6,5 }; 65 | vector vec(arr, arr + 7); 66 | 67 | cout < 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | bool match(string s, string p) { 10 | /** 11 | * f[i][j]: if s[0..i-1] matches p[0..j-1] 12 | * if p[j - 1] != '*' 13 | * f[i][j] = f[i - 1][j - 1] && s[i - 1] == p[j - 1] 14 | * if p[j - 1] == '*', denote p[j - 2] with x 15 | * f[i][j] is true iff any of the following is true 16 | * 1) "x*" repeats 0 time and matches empty: f[i][j - 2] 17 | * 2) "x*" repeats >= 1 times and matches "x*x": s[i - 1] == x && f[i - 1][j] 18 | * '.' matches any single character 19 | */ 20 | int m = s.size(), n = p.size(); 21 | vector> f(m + 1, vector(n + 1, false)); 22 | 23 | f[0][0] = true; 24 | for (int i = 1; i <= m; i++) 25 | f[i][0] = false; 26 | // p[0.., j - 3, j - 2, j - 1] matches empty iff p[j - 1] is '*' and p[0..j - 3] matches empty 27 | for (int j = 1; j <= n; j++) 28 | f[0][j] = j > 1 && '*' == p[j - 1] && f[0][j - 2]; 29 | 30 | for (int i = 1; i <= m; i++) 31 | for (int j = 1; j <= n; j++) 32 | if (p[j - 1] != '*') 33 | f[i][j] = f[i - 1][j - 1] && (s[i - 1] == p[j - 1] || '.' == p[j - 1]); 34 | else 35 | // p[0] cannot be '*' so no need to check "j > 1" here 36 | f[i][j] = f[i][j - 2] || (s[i - 1] == p[j - 2] || '.' == p[j - 2]) && f[i - 1][j]; 37 | 38 | return f[m][n]; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /042-左旋转字符串/01-rotate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // 调试开关 6 | #define __tmain main 7 | 8 | #ifdef __tmain 9 | 10 | #define debug cout 11 | 12 | #else 13 | 14 | #define debug 0 && cout 15 | 16 | #endif // __tmain 17 | 18 | class Solution 19 | { 20 | public: 21 | string LeftRotateString(string str, int n) 22 | { 23 | if(str.size( ) == 0) 24 | { 25 | return ""; 26 | } 27 | /// 012345678 28 | /// abcXYZdef 29 | /// XYZdefabc 30 | /// 012345678 31 | /// 32 | /// res[i] = str[(i + n) % str.size( )]; 33 | /// res[(i - n + str.size( )) % str.size()] = str[i]; 34 | 35 | string res(str); 36 | if(n > str.size( )) 37 | { 38 | n %= str.size( ); 39 | } 40 | debug <<"length = " < 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | #ifdef __tmain 21 | 22 | struct ListNode 23 | { 24 | int val; 25 | struct ListNode *next; 26 | }; 27 | 28 | 29 | #endif // __tmain 30 | 31 | #define SLOW_STEP 1 32 | #define FAST_STEP 2 33 | 34 | class Solution 35 | { 36 | public: 37 | ListNode* EntryNodeOfLoop(ListNode* pHead) 38 | { 39 | if (pHead == NULL 40 | || pHead->next == NULL) 41 | { 42 | return NULL; 43 | } 44 | 45 | ListNode* previous = pHead; 46 | ListNode* current = pHead->next; 47 | while (current != NULL) 48 | { 49 | previous->next = NULL; 50 | previous = current; 51 | current = current->next; 52 | } 53 | 54 | return previous; 55 | } 56 | }; 57 | 58 | int __tmain( ) 59 | { 60 | Solution solu; 61 | 62 | ListNode list[6]; 63 | 64 | list[0].val = 1; 65 | list[0].next = &list[1]; 66 | 67 | list[1].val = 2; 68 | list[1].next = &list[2]; 69 | 70 | list[2].val = 3; 71 | list[2].next = &list[3]; 72 | 73 | list[3].val = 4; 74 | list[3].next = &list[4]; 75 | 76 | list[4].val = 5; 77 | list[4].next = &list[5]; 78 | 79 | list[5].val = 6; 80 | list[5].next = &list[2]; 81 | 82 | cout <val < 2 | #include 3 | using namespace std; 4 | 5 | // 调试开关 6 | #define __tmain main 7 | 8 | #ifdef __tmain 9 | 10 | #define debug cout 11 | 12 | #else 13 | 14 | #define debug 0 && cout 15 | 16 | #endif // __tmain 17 | 18 | 19 | class Solution 20 | { 21 | public: 22 | 23 | string ReverseSentence(string str) 24 | { 25 | if (str.size( ) == 0) 26 | { 27 | return str; 28 | } 29 | 30 | reverse(str.begin(), str.end()); 31 | 32 | size_t left = 0; 33 | size_t right = 0; 34 | while(right != string::npos) 35 | { 36 | left = str.find_first_not_of(' ', left); // 第一个非空格字符是单词的起始位置 37 | right = str.find_first_of(' ', left); // 第一个空格位置标识了单词的结束 38 | 39 | // 处理末尾是空格(原字符串开头是空格的情况) 40 | // 当最后全是空格的时候,此时可以结束循环 41 | if (left == string::npos) 42 | { 43 | break; 44 | } 45 | // 如果查找不到空格, 到了字符串的末尾 46 | // 此时[degin, str.size( )]是末尾的字符串 47 | if (right == string::npos) 48 | { 49 | reverse(str.begin( ) + left, str.end( )); 50 | } 51 | else // 否则[left, end]是一个单词 52 | { 53 | reverse(str.begin( ) + left, str.begin() + right); 54 | left = right + 1; 55 | } 56 | } 57 | 58 | return str; 59 | } 60 | }; 61 | 62 | int __tmain( ) 63 | { 64 | Solution solu; 65 | cout < 2 | #include 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | class Solution 21 | { 22 | public: 23 | Solution() 24 | { 25 | str = ""; 26 | memset(count, '\0', sizeof(count)); 27 | } 28 | 29 | // Insert one char from stringstream 30 | void Insert(char ch) 31 | { 32 | str += ch; 33 | count[(int)ch]++; 34 | } 35 | 36 | // Return the first appearence once char in current stringstream 37 | char FirstAppearingOnce() 38 | { 39 | int len = str.size( ); 40 | 41 | for(int i = 0; i < len; i++) 42 | { 43 | if(count[(int)str[i]] == 1) 44 | { 45 | return str[i]; 46 | } 47 | } 48 | 49 | return '#'; 50 | } 51 | 52 | private: 53 | std::string str; 54 | int count[256]; 55 | 56 | 57 | }; 58 | 59 | int __tmain( ) 60 | { 61 | Solution solu; 62 | 63 | solu.Insert('g'); 64 | cout < 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | using namespace std; 11 | 12 | 13 | // 调试开关 14 | #define __tmain main 15 | 16 | #ifdef __tmain 17 | 18 | #define debug cout 19 | 20 | #else 21 | 22 | #define debug 0 && cout 23 | 24 | #endif // __tmain 25 | 26 | 27 | 28 | /* 29 | * string to int 30 | */ 31 | // 使用atoi将char*转换为整数 32 | int StringToIntByatoi(string str) 33 | { 34 | int num = atoi(str.c_str()); 35 | debug <<"str(" <>num; 56 | 57 | debug <<"str(" <>num; 70 | 71 | debug <<"str(" < 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // 调试开关 8 | #define __tmain main 9 | 10 | #ifdef __tmain 11 | 12 | #define debug cout 13 | 14 | #else 15 | 16 | #define debug 0 && cout 17 | 18 | #endif // __tmain 19 | 20 | 21 | class Solution 22 | { 23 | public: 24 | int LastRemaining_Solution(unsigned int n, unsigned int m) 25 | { 26 | if(n < 1 || m < 1) 27 | { 28 | return -1; 29 | } 30 | 31 | int last = 0; 32 | debug <<"F[" <<0 <<"] = " < 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | 11 | // 调试开关 12 | #define __tmain main 13 | 14 | #ifdef __tmain 15 | 16 | #define debug cout 17 | 18 | #else 19 | 20 | #define debug 0 && cout 21 | 22 | #endif // __tmain 23 | 24 | /* 单调队列,O(n) 25 | * 引用马客(Mark)的解题思路,马客没加注释,我用自己的理解加下注释,希望对你们有用, 26 | * 如有错误,见谅,我会及时修改 27 | * deque s中存储的是num的下标 */ 28 | 29 | class Solution 30 | { 31 | public: 32 | vector maxInWindows(const vector& num, unsigned int size) 33 | { 34 | vector res; 35 | if(num.size() == 0 || size == 0) 36 | { 37 | return res; 38 | } 39 | 40 | for(int start = 0; 41 | start <= (int)(num.size( ) - size); 42 | start++) /* 循环所有窗口的开始位置 */ 43 | { 44 | int end = start + size; /* 窗口的结束位置 */ 45 | 46 | /* 找出滑动窗口的最大值 */ 47 | int max = INT_MIN; 48 | for(int index = start; index < end; index++) 49 | { 50 | if(num[index] > max) 51 | { 52 | max = num[index]; 53 | } 54 | } 55 | 56 | debug <<"[" < 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | using namespace std; 8 | 9 | 10 | // 调试开关 11 | #define __tmain main 12 | 13 | #ifdef __tmain 14 | 15 | #define debug cout 16 | 17 | #else 18 | 19 | #define debug 0 && cout 20 | 21 | #endif // __tmain 22 | 23 | class Solution 24 | { 25 | public : 26 | typedef pair Pair; 27 | 28 | vector maxInWindows(const vector& num, unsigned int size) 29 | { 30 | vector res; 31 | if(size == 0 || num.size( ) == 0 || size > num.size( )) 32 | { 33 | return res; 34 | } 35 | 36 | priority_queue Q; //优先级队列保存窗口里面的值 37 | for (int i = 0; i < size; i++) 38 | { 39 | Q.push(Pair(num[i], i)); //构建size个元素的最大堆 40 | } 41 | 42 | for (int i = size; 43 | i < num.size( ); 44 | i++) 45 | { 46 | //Q.push(Pair(num[i], i)); //构建size个元素的最大堆 47 | Pair p = Q.top( ); 48 | res.push_back(p.first); //res[i - size] = p.first; 49 | while (p.second <= (i - size)) 50 | { 51 | Q.pop( ); 52 | p = Q.top( ); 53 | } 54 | Q.push(Pair(num[i], i)); //res[num.size( ) - size] = Q.top( ).first; 55 | } 56 | res.push_back(Q.top( ).first); //res[num.size( ) - size] = Q.top( ).first; 57 | 58 | return res; 59 | } 60 | }; 61 | 62 | int __tmain( ) 63 | { 64 | Solution solu; 65 | 66 | int array[] = { 2, 3, 4, 6}; 67 | vector vec(array, array + 4); 68 | 69 | vector res = solu.maxInWindows(vec, 3); 70 | copy(res.begin( ), res.end( ), ostream_iterator(cout," ")); 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/Utilities/BinaryTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "BinaryTree.h" 3 | 4 | BinaryTreeNode* CreateBinaryTreeNode(int value) 5 | { 6 | BinaryTreeNode* pNode = new BinaryTreeNode(); 7 | pNode->m_nValue = value; 8 | pNode->m_pLeft = NULL; 9 | pNode->m_pRight = NULL; 10 | 11 | return pNode; 12 | } 13 | 14 | void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight) 15 | { 16 | if(pParent != NULL) 17 | { 18 | pParent->m_pLeft = pLeft; 19 | pParent->m_pRight = pRight; 20 | } 21 | } 22 | 23 | void PrintTreeNode(BinaryTreeNode* pNode) 24 | { 25 | if(pNode != NULL) 26 | { 27 | printf("value of this node is: %d\n", pNode->m_nValue); 28 | 29 | if(pNode->m_pLeft != NULL) 30 | printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue); 31 | else 32 | printf("left child is null.\n"); 33 | 34 | if(pNode->m_pRight != NULL) 35 | printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue); 36 | else 37 | printf("right child is null.\n"); 38 | } 39 | else 40 | { 41 | printf("this node is null.\n"); 42 | } 43 | 44 | printf("\n"); 45 | } 46 | 47 | void PrintTree(BinaryTreeNode* pRoot) 48 | { 49 | PrintTreeNode(pRoot); 50 | 51 | if(pRoot != NULL) 52 | { 53 | if(pRoot->m_pLeft != NULL) 54 | PrintTree(pRoot->m_pLeft); 55 | 56 | if(pRoot->m_pRight != NULL) 57 | PrintTree(pRoot->m_pRight); 58 | } 59 | } 60 | 61 | void DestroyTree(BinaryTreeNode* pRoot) 62 | { 63 | if(pRoot != NULL) 64 | { 65 | BinaryTreeNode* pLeft = pRoot->m_pLeft; 66 | BinaryTreeNode* pRight = pRoot->m_pRight; 67 | 68 | delete pRoot; 69 | pRoot = NULL; 70 | 71 | DestroyTree(pLeft); 72 | DestroyTree(pRight); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /035-第一个只出现一次的字符位置/01-bruteforce.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | using namespace std; 6 | 7 | 8 | #define __tmain main 9 | 10 | #ifdef __tmain 11 | 12 | #define debug cout 13 | 14 | #else 15 | 16 | #define debug 0 && cout 17 | 18 | #endif // __tmain 19 | 20 | 21 | 22 | class Solution 23 | { 24 | 25 | public: 26 | 27 | int FirstNotRepeatingChar(string str) 28 | { 29 | if(str.length( ) == 0) 30 | { 31 | return -1; 32 | } 33 | 34 | unsigned int length = str.length( ); 35 | unsigned int i, j; 36 | bool isrepeat = false; 37 | 38 | 39 | for(i = 0; i < length; i++) 40 | { 41 | isrepeat = false; 42 | if(str[i] == '\0') 43 | { 44 | continue; 45 | } 46 | debug <<"str[" < 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // 调试开关 8 | #define __tmain main 9 | 10 | #ifdef __tmain 11 | 12 | #define debug cout 13 | 14 | #else 15 | 16 | #define debug 0 && cout 17 | 18 | #endif // __tmain 19 | 20 | class Solution 21 | { 22 | public: 23 | bool IsContinuous(vector numbers) 24 | { 25 | if(numbers.size( ) == 0) 26 | { 27 | return false; 28 | } 29 | sort(numbers.begin( ), numbers.end( )); 30 | 31 | /// 统计前面0的个数 32 | int left = 0; 33 | while(numbers[left] == 0) 34 | { 35 | left++; 36 | } 37 | debug <<"left = " <>str; 66 | 67 | debug <<"int(" < 2 | 3 | using namespace std; 4 | 5 | // 调试开关 6 | #define __tmain main 7 | 8 | #ifdef __tmain 9 | 10 | #define debug cout 11 | 12 | #else 13 | 14 | #define debug 0 && cout 15 | 16 | #endif // __tmain 17 | 18 | class Solution 19 | { 20 | public: 21 | string ReverseSentence(string str) 22 | { 23 | if(str.size( ) == 0) 24 | { 25 | return ""; 26 | } 27 | 28 | // 首先翻转整个句子 29 | Reverse(str, 0, str.size( ) - 1); 30 | 31 | // 紧接着查找到每个单词, 然后翻转它 32 | int left = 0, right = 0; 33 | while(right <= str.size( )) 34 | { 35 | // 当遇见空格的时候说明找到了一个单词 36 | // 当走到字符串的最后一个位置, 那么前面如果不是空格, 那么最后也会有一个单词 37 | if((str[right] == ' ') 38 | || (right == str.size( ) && str[right - 1] != ' ')) 39 | { 40 | /* 41 | for(int i = left; i < right; i++) 42 | { 43 | debug < 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // 调试开关 8 | #define __tmain main 9 | 10 | #ifdef __tmain 11 | 12 | #define debug cout 13 | 14 | #else 15 | 16 | #define debug 0 && cout 17 | 18 | #endif // __tmain 19 | 20 | 21 | class Solution 22 | { 23 | public: 24 | int LastRemaining_Solution(unsigned int n, unsigned int m) 25 | { 26 | if(n < 1 || m < 1) 27 | { 28 | return -1; 29 | } 30 | 31 | 32 | vector numbers; 33 | 34 | for(int i = 0; i < n; i++) 35 | { 36 | numbers.push_back(i); 37 | debug < 1) 44 | { 45 | debug <<"count = " <<1 <<", num[" <(cout, " ")); 60 | debug <(cout," ")); 65 | swap(str[i], str[begin]); 66 | } 67 | } 68 | } 69 | } 70 | 71 | private: 72 | //find duplicate of str[i] in str[k,i) 73 | bool HasDuplicate(string& str, int k, int i) const { 74 | for (int p = k; p < i; p++) 75 | if (str[p] == str[i]) return true; 76 | 77 | return false; 78 | } 79 | }; 80 | 81 | int __tmain( ) 82 | { 83 | Solution solu; 84 | solu.Permutation("abcd"); 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /042-翻转单词顺序列/03-reverse_stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // 调试开关 6 | #define __tmain main 7 | 8 | #ifdef __tmain 9 | 10 | #define debug cout 11 | 12 | #else 13 | 14 | #define debug 0 && cout 15 | 16 | #endif // __tmain 17 | 18 | 19 | //使用两个栈 20 | class Solution 21 | { 22 | public: 23 | string ReverseSentence(string str) 24 | { 25 | if (str.size( ) == 0) 26 | { 27 | return str; 28 | } 29 | 30 | string result; 31 | stack stk1; 32 | stack stk2; 33 | 34 | for (auto i : str) 35 | { //把str中的全部压入 36 | debug <<"push in stack_1 "< 2 | #include 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | class Solution 21 | { 22 | public: 23 | // Parameters: 24 | // numbers: an array of integers 25 | // length: the length of array numbers 26 | // duplication: (Output) the duplicated number in the array number 27 | // Return value: true if the input is valid, and there are some duplications in the array number 28 | // otherwise false 29 | bool duplicate(int numbers[], int length, int* duplication) 30 | { 31 | *duplication = -1; 32 | 33 | if(CheckValidity(numbers, length) == false) 34 | { 35 | return false; 36 | } 37 | int flags[length]; 38 | memset(flags, 0, sizeof(flags)); 39 | for(int i = 0; i < length; i++) 40 | { 41 | if(flags[numbers[i]] != 0) 42 | { 43 | *duplication = numbers[i]; 44 | return true; 45 | } 46 | else 47 | { 48 | flags[numbers[i]] = 1; 49 | } 50 | } 51 | 52 | return false; 53 | } 54 | 55 | bool CheckValidity(int *numbers, int length) 56 | { 57 | // 输入数据不合法 58 | if(numbers == NULL || length <= 0) 59 | { 60 | return false; 61 | } 62 | 63 | // 元素必须在[0, n-1]的范围 64 | for(int i = 0; i < length; i++) 65 | { 66 | if(numbers[i] < 0 || numbers[i] > length - 1) 67 | { 68 | return false; 69 | } 70 | } 71 | 72 | return true; 73 | } 74 | 75 | }; 76 | 77 | 78 | int __tmain( ) 79 | { 80 | Solution solu; 81 | int result; 82 | 83 | int array[] = {2, 3, 1, 0, 2, 5, 3}; 84 | //int array[] = { 2, 1, 3, 0, 4 }; 85 | if(solu.duplicate(array, 5, &result) == true) 86 | { 87 | cout < 2 | 3 | using namespace std; 4 | 5 | #define __tmain main 6 | 7 | class Solution 8 | { 9 | public: 10 | bool match(char* str, char* regex) 11 | { 12 | if(*str == NULL) // 空串的情况, 可以匹配' ', '*' 13 | { 14 | if(*regex == NULL 15 | || (*(regex + 1) == '*' && match(str, regex + 2))) 16 | { 17 | return true; 18 | } 19 | else 20 | { 21 | return false; 22 | } 23 | } 24 | else if(*regex == NULL) 25 | { 26 | return false; 27 | } 28 | 29 | if(*(regex + 1) != '*') 30 | { 31 | if(*str == *regex || *regex == '.') // 对应字符相等或者可以匹配 32 | { 33 | return match(str + 1, regex + 1); 34 | } 35 | else 36 | { 37 | return false; 38 | } 39 | } 40 | else 41 | { 42 | if(*str != *regex && *regex != '.') 43 | { 44 | // 匹配 45 | return match(str, regex + 2); 46 | } 47 | else 48 | { 49 | 50 | //try matches 0 char, skip p and p+1 ('*') 51 | if(match(str, regex + 2)) 52 | { 53 | return true; 54 | } 55 | 56 | int i = 0; 57 | while(*(str + i) == *regex || *regex == '.') 58 | { 59 | //try all the length '*' matches 60 | if(match(str + i + 1, regex + 2)) 61 | { 62 | return true; 63 | } 64 | 65 | if(*(str + i + 1) == 0) 66 | { 67 | break; 68 | } 69 | i ++; 70 | } 71 | 72 | return false; 73 | 74 | } 75 | } 76 | } 77 | }; 78 | 79 | 80 | int __tmain(void) 81 | { 82 | 83 | 84 | return EXIT_SUCCESS; 85 | 86 | } 87 | 88 | 89 | -------------------------------------------------------------------------------- /051-数组中重复的数字/addlength.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | 21 | class Solution 22 | { 23 | public: 24 | // Parameters: 25 | // numbers: an array of integers 26 | // length: the length of array numbers 27 | // duplication: (Output) the duplicated number in the array number 28 | // Return value: true if the input is valid, and there are some duplications in the array number 29 | // otherwise false 30 | bool duplicate(int numbers[], int length, int* duplication) 31 | { 32 | *duplication = -1; 33 | 34 | if(CheckValidity(numbers, length) == false) 35 | { 36 | return false; 37 | } 38 | 39 | for(int i = 0; i < length; i++) 40 | { 41 | if(numbers[numbers[i] % length] >= length) 42 | { 43 | *duplication = numbers[i] % length; 44 | return true; 45 | } 46 | else 47 | { 48 | numbers[numbers[i] % length] += length; 49 | } 50 | } 51 | 52 | return false; 53 | 54 | } 55 | 56 | bool CheckValidity(int *numbers, int length) 57 | { 58 | // 输入数据不合法 59 | if(numbers == NULL || length <= 0) 60 | { 61 | return false; 62 | } 63 | 64 | // 元素必须在[0, n-1]的范围 65 | for(int i = 0; i < length; i++) 66 | { 67 | if(numbers[i] < 0 || numbers[i] > length - 1) 68 | { 69 | return false; 70 | } 71 | } 72 | 73 | return true; 74 | } 75 | 76 | }; 77 | 78 | 79 | int __tmain( ) 80 | { 81 | Solution solu; 82 | int result; 83 | 84 | //int array[] = {2, 3, 1, 0, 2, 5, 3}; 85 | int array[] = { 2, 1, 3, 1, 4 }; 86 | if(solu.duplicate(array, 5, &result) == true) 87 | { 88 | cout < 2 | #include 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | 21 | class Solution 22 | { 23 | public: 24 | // Parameters: 25 | // numbers: an array of integers 26 | // length: the length of array numbers 27 | // duplication: (Output) the duplicated number in the array number 28 | // Return value: true if the input is valid, and there are some duplications in the array number 29 | // otherwise false 30 | bool duplicate(int numbers[], int length, int* duplication) 31 | { 32 | *duplication = -1; 33 | 34 | if(CheckValidity(numbers, length) == false) 35 | { 36 | return false; 37 | } 38 | 39 | sort(numbers, numbers + length); // 对数组进行排序 40 | 41 | int i = 0; 42 | bool isDup = false; 43 | *duplication = -1; 44 | 45 | // 扫描一遍数组发现重复的数字 46 | for(i = 0; i < length - 1; i++) 47 | { 48 | if(numbers[i] == numbers[i + 1]) // 重复了 49 | { 50 | isDup = true; 51 | *duplication = numbers[i]; 52 | break; 53 | } 54 | } 55 | 56 | return isDup; 57 | } 58 | 59 | bool CheckValidity(int *numbers, int length) 60 | { 61 | // 输入数据不合法 62 | if(numbers == NULL || length <= 0) 63 | { 64 | return false; 65 | } 66 | 67 | // 元素必须在[0, n-1]的范围 68 | for(int i = 0; i < length; i++) 69 | { 70 | if(numbers[i] < 0 || numbers[i] > length - 1) 71 | { 72 | return false; 73 | } 74 | } 75 | 76 | return true; 77 | } 78 | }; 79 | 80 | 81 | int __tmain( ) 82 | { 83 | Solution solu; 84 | int result; 85 | 86 | int array[] = {2, 3, 1, 0, 2, 5, 3}; 87 | if(solu.duplicate(array, 7, &result) == true) 88 | { 89 | cout < 2 | 3 | using namespace std; 4 | 5 | #define __tmain main 6 | 7 | #ifdef __tmain 8 | 9 | #define debug cout 10 | 11 | #else 12 | 13 | #define debug 0 && cout 14 | 15 | #endif // __tmain 16 | 17 | class Solution 18 | { 19 | public: 20 | /** 21 | * [NumberOf1Between1AndN_Solution 统计1到n的整数中1出现的次数] 22 | * @Author gatieme 23 | * @DateTime 2016-05-01T22:03:43+0800 24 | * @param n [整数n] 25 | * @return [1到n的整数中1出现的次数] 26 | */ 27 | int NumberOf1Between1AndN_Solution(int n) 28 | { 29 | long count = 0; 30 | 31 | long i = 1; 32 | 33 | long current = 0,after = 0,before = 0; 34 | 35 | while((n / i) != 0) 36 | { 37 | before = n / (i * 10); 38 | current = (n / i) % 10; 39 | after = n - (n / i) * i; 40 | 41 | debug <<"n = " << n <<", " <= 1) 44 | { 45 | // 如果current大于1,则出现1的次数为其高位before的数字加1 46 | count += (before + 1) * i; 47 | debug <<"count += (" <(cout," ")); 47 | cout < size) 58 | { 59 | index.pop_front( ); 60 | } 61 | 62 | /* 把每次滑动的num下标加入队列 */ 63 | index.push_back(i); 64 | 65 | /* 当滑动窗口首地址i大于等于size时才开始写入窗口最大值 */ 66 | if(size != 0 && i + 1 >= size) 67 | { 68 | res.push_back(num[index.front( )]); 69 | } 70 | } 71 | return res; 72 | } 73 | }; 74 | 75 | 76 | 77 | int __tmain( ) 78 | { 79 | Solution solu; 80 | 81 | int array[] = { 2, 3, 4, 2, 6, 2, 5, 1 }; 82 | vector vec(array, array + 8); 83 | 84 | vector res = solu.maxInWindows(vec, 3); 85 | copy(res.begin( ), res.end( ), ostream_iterator(cout," ")); 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /051-数组中重复的数字/hashset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | 21 | class Solution 22 | { 23 | public: 24 | // Parameters: 25 | // numbers: an array of integers 26 | // length: the length of array numbers 27 | // duplication: (Output) the duplicated number in the array number 28 | // Return value: true if the input is valid, and there are some duplications in the array number 29 | // otherwise false 30 | bool duplicate(int numbers[], int length, int* duplication) 31 | { 32 | 33 | *duplication = -1; 34 | 35 | if(CheckValidity(numbers, length) == false) 36 | { 37 | return false; 38 | } 39 | 40 | set s; 41 | bool isDup = false; 42 | *duplication = -1; 43 | for(int i = 0; i < length; i++) 44 | { 45 | if(s.count(numbers[i]) == 1) // 如果当前元素已经存在set中 46 | { 47 | isDup = true; 48 | *duplication = numbers[i]; 49 | break; 50 | } 51 | s.insert(numbers[i]); 52 | 53 | } 54 | 55 | return isDup; 56 | } 57 | 58 | bool CheckValidity(int *numbers, int length) 59 | { 60 | // 输入数据不合法 61 | if(numbers == NULL || length <= 0) 62 | { 63 | return false; 64 | } 65 | 66 | // 元素必须在[0, n-1]的范围 67 | for(int i = 0; i < length; i++) 68 | { 69 | if(numbers[i] < 0 || numbers[i] > length - 1) 70 | { 71 | return false; 72 | } 73 | } 74 | 75 | return true; 76 | } 77 | }; 78 | struct Bit 79 | { 80 | bool a:1; 81 | char b:4; 82 | }; 83 | 84 | 85 | int __tmain( ) 86 | { 87 | Solution solu; 88 | int result; 89 | 90 | int array[] = {2, 3, 1, 0, 2, 5, 3}; 91 | if(solu.duplicate(array, 7, &result) == true) 92 | { 93 | cout < 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | #ifdef __tmain 21 | 22 | struct ListNode 23 | { 24 | int val; 25 | struct ListNode *next; 26 | ListNode(int x = 0) 27 | : 28 | val(x), next(NULL) { 29 | } 30 | }; 31 | #endif // __tmain 32 | 33 | 34 | class Solution 35 | { 36 | public: 37 | ListNode* deleteDuplication(ListNode* pHead) 38 | { 39 | ListNode h(-1); 40 | ListNode *newNode = &h; 41 | ListNode *preNode = NULL; 42 | ListNode *oldNode = pHead; 43 | int cnt; 44 | 45 | while(oldNode != NULL) 46 | { 47 | preNode = oldNode; 48 | oldNode = oldNode->next; 49 | cnt = 0; 50 | 51 | // 对preNode出现的次数进行计数 52 | while(oldNode && oldNode->val == preNode->val) 53 | { 54 | oldNode = oldNode->next; 55 | ++cnt; 56 | } 57 | 58 | // 如果preNode出现了0次 59 | if(cnt == 0) 60 | { 61 | // 把preNode链接到了newNode的末尾 62 | newNode->next = preNode; 63 | newNode = preNode; 64 | } 65 | } 66 | newNode->next = NULL; 67 | return h.next; 68 | } 69 | 70 | }; 71 | 72 | 73 | int __tmain( ) 74 | { 75 | Solution solu; 76 | 77 | ListNode list[7]; 78 | 79 | list[0].val = 1; 80 | list[0].next = &list[1]; 81 | 82 | list[1].val = 2; 83 | list[1].next = &list[2]; 84 | 85 | list[2].val = 3; 86 | list[2].next = &list[3]; 87 | 88 | list[3].val = 3; 89 | list[3].next = &list[4]; 90 | 91 | list[4].val = 4; 92 | list[4].next = &list[5]; 93 | 94 | list[5].val = 4; 95 | list[5].next = &list[6]; 96 | 97 | list[6].val = 5; 98 | list[6].next = NULL; 99 | 100 | 101 | ListNode* node = solu.deleteDuplication(list); 102 | 103 | while(node != NULL) 104 | { 105 | cout <val <<" -=> "; 106 | node = node->next; 107 | } 108 | return 0; 109 | } 110 | -------------------------------------------------------------------------------- /031-连续子数组的最大和/02-dp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | 10 | #define __tmain main 11 | 12 | #ifdef __tmain 13 | 14 | #define debug cout 15 | 16 | #else 17 | 18 | #define debug 0 && cout 19 | 20 | #endif // __tmain 21 | 22 | 23 | class Solution 24 | { 25 | int dp[1000]; 26 | public: 27 | int FindGreatestSumOfSubArray(vector array) 28 | { 29 | if(array.size( ) == 0) 30 | { 31 | return 0; 32 | } 33 | 34 | #ifdef __tmain 35 | debug <<"vector : "; 36 | copy(array.begin( ), array.end( ), ostream_iterator(cout, " ")); 37 | debug < 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | #ifdef __tmain 21 | 22 | struct TreeNode 23 | { 24 | int val; 25 | struct TreeNode *left; 26 | struct TreeNode *right; 27 | 28 | TreeNode(int x = 0) 29 | :val(x), left(NULL), right(NULL) 30 | { 31 | } 32 | }; 33 | 34 | #endif 35 | 36 | 37 | class Solution 38 | { 39 | public: 40 | bool isSymmetrical(TreeNode* pRoot) 41 | { 42 | if(pRoot == NULL) 43 | { 44 | return true; 45 | } 46 | return isSymmetricalRecursion(pRoot->left, pRoot->right); 47 | } 48 | 49 | bool isSymmetricalRecursion(TreeNode *pLeft, TreeNode *pRight) 50 | { 51 | if(pLeft == NULL && pRight == NULL) 52 | { 53 | return true; 54 | } 55 | 56 | if(pLeft == NULL || pRight == NULL) 57 | { 58 | return false; 59 | } 60 | 61 | if(pLeft->val != pRight->val) 62 | { 63 | return false; 64 | } 65 | 66 | // 左子树的左与右子树的右对称 67 | // 左子树的右与右子树的左对称 68 | return isSymmetricalRecursion(pLeft->left, pRight->right) 69 | && isSymmetricalRecursion(pLeft->right, pRight->left); 70 | } 71 | }; 72 | 73 | int __tmain( ) 74 | { 75 | 76 | 77 | TreeNode tree[7]; 78 | 79 | tree[0].val = 8; 80 | tree[0].left = &tree[1]; 81 | tree[0].right = &tree[2]; 82 | 83 | 84 | tree[1].val = 6; 85 | tree[1].left = &tree[3]; 86 | tree[1].right = &tree[4]; 87 | 88 | 89 | tree[2].val = 6; 90 | tree[2].left = &tree[5]; 91 | tree[2].right = &tree[6]; 92 | 93 | 94 | tree[3].val = 5; 95 | tree[3].left = NULL; 96 | tree[3].right = NULL; 97 | 98 | tree[4].val = 7; 99 | tree[4].left = NULL; 100 | tree[4].right = NULL; 101 | 102 | tree[5].val = 7; 103 | tree[5].left = NULL; 104 | tree[5].right = NULL; 105 | 106 | tree[6].val = 5; 107 | tree[6].left = NULL; 108 | tree[6].right = NULL; 109 | 110 | 111 | Solution solu; 112 | cout < 2 | #include 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | 21 | #define SET_SYMBOL_BIT(num) ((num) |= (1 << 31)) /* 设置符号位为1 */ 22 | #define GET_ORIGIN_NUM(num) ((num) & (~(1 << 31))) /* 获取到源数据 */ 23 | #define GET_SYMBOL_BIT(num) (((num) >> 31) & 1) /* 获取符号位(标识)*/ 24 | 25 | class Solution 26 | { 27 | public: 28 | /* 29 | 思路1 : 30 | 对于每一个数据numbers[i] 31 | 其源数据origin = GET_ORIGIN_NUM(numbers[i])的标识存储在numbers[origin]的符号位 32 | 判断其标识是否为1即可 33 | */ 34 | bool duplicate(int numbers[], int length, int* duplication) 35 | { 36 | *duplication = -1; 37 | 38 | if(CheckValidity(numbers, length) == false) 39 | { 40 | return false; 41 | } 42 | for(int i = 0; i < length; i++) 43 | { 44 | // 当前数字numbers[i]的标识即是numbers[numbers[i]]的符号位 45 | // 检查numbers[i] 46 | debug <<"origin = " < 2 | 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | #ifdef __tmain 21 | 22 | struct ListNode 23 | { 24 | int val; 25 | struct ListNode *next; 26 | ListNode(int x = 0) 27 | : 28 | val(x), next(NULL) { 29 | } 30 | }; 31 | #endif // __tmain 32 | 33 | 34 | class Solution 35 | { 36 | public: 37 | ListNode* deleteDuplication(ListNode* pHead) 38 | { 39 | //设置一个trick, 作为头指针, 这样我们无需单独考虑第一个元素 40 | ListNode *first = new ListNode(-1); 41 | 42 | first->next = pHead; 43 | 44 | ListNode *p = pHead; 45 | ListNode *last = first; 46 | 47 | while (p != NULL && p->next != NULL) 48 | { 49 | debug <val <val == p->next->val) 53 | { 54 | // 就跳过所有重复的数字 55 | int val = p->val; 56 | while (p != NULL && p->val == val) 57 | { 58 | p = p->next; 59 | } 60 | 61 | // 此时p指向了非重复的第一个元素 62 | // 我们设置last->next = p 63 | // 但是此时p-val也可能是重复的, 64 | // 因此我们不可以设置last = p 65 | // 而是重新进入循环 66 | last->next = p; 67 | } 68 | else 69 | { 70 | last = p; 71 | p = p->next; 72 | } 73 | } 74 | return first->next; 75 | } 76 | }; 77 | 78 | int __tmain( ) 79 | { 80 | Solution solu; 81 | 82 | ListNode list[7]; 83 | 84 | list[0].val = 1; 85 | list[0].next = &list[1]; 86 | 87 | list[1].val = 2; 88 | list[1].next = &list[2]; 89 | 90 | list[2].val = 3; 91 | list[2].next = &list[3]; 92 | 93 | list[3].val = 3; 94 | list[3].next = &list[4]; 95 | 96 | list[4].val = 4; 97 | list[4].next = &list[5]; 98 | 99 | list[5].val = 4; 100 | list[5].next = &list[6]; 101 | 102 | list[6].val = 5; 103 | list[6].next = NULL; 104 | 105 | 106 | ListNode *node = solu.deleteDuplication(list); 107 | 108 | while(node != NULL) 109 | { 110 | cout <val <<" -=> "; 111 | node = node->next; 112 | } 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /064-数据流之中的中位数/multiset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | 9 | 10 | // 调试开关 11 | #define __tmain main 12 | 13 | #ifdef __tmain 14 | 15 | #define debug cout 16 | 17 | #else 18 | 19 | #define debug 0 && cout 20 | 21 | #endif // __tmain 22 | 23 | 24 | 25 | class Solution 26 | { 27 | protected : 28 | multiset left; /* 左半部分 */ 29 | multiset right; /* 右半部分 */ 30 | 31 | public : 32 | 33 | void Insert(int n) 34 | { 35 | int tmp = n; 36 | if(((left.size( ) + right.size( )) & 1) == 0) 37 | { 38 | if (right.empty( ) != true && n > *right.begin()) 39 | { 40 | right.insert(n); 41 | tmp = *right.begin( ); 42 | right.erase(right.find(tmp)); 43 | } 44 | left.insert(tmp); 45 | } 46 | else 47 | { 48 | if (left.empty() != true && n < *left.rbegin()) 49 | { 50 | left.insert(n); 51 | tmp = *left.rbegin(); 52 | left.erase(left.find(tmp)); 53 | } 54 | right.insert(tmp); 55 | } 56 | 57 | } 58 | 59 | double GetMedian( ) 60 | { 61 | #ifdef __tmain 62 | cout <<"left[" <(cout," ")); 64 | cout <<"right[" <(cout," ")); 66 | cout < vec(array, array + 9); 88 | 89 | /* 90 | vector res = s.GetMedian(vec); 91 | for(int i = 0; i < (int)res.size( ); i++) 92 | { 93 | cout < 2 | #include 3 | #include "list.h" 4 | 5 | ListNode* CreateListNode(int value) 6 | { 7 | ListNode* pNode = new ListNode(); 8 | pNode->m_nValue = value; 9 | pNode->m_pNext = NULL; 10 | 11 | return pNode; 12 | } 13 | 14 | void ConnectListNodes(ListNode* pCurrent, ListNode* pNext) 15 | { 16 | if(pCurrent == NULL) 17 | { 18 | printf("Error to connect two nodes.\n"); 19 | exit(1); 20 | } 21 | 22 | pCurrent->m_pNext = pNext; 23 | } 24 | 25 | void PrintListNode(ListNode* pNode) 26 | { 27 | if(pNode == NULL) 28 | { 29 | printf("The node is NULL\n"); 30 | } 31 | else 32 | { 33 | printf("The key in node is %d.\n", pNode->m_nValue); 34 | } 35 | } 36 | 37 | void PrintList(ListNode* pHead) 38 | { 39 | printf("PrintList starts.\n"); 40 | 41 | ListNode* pNode = pHead; 42 | while(pNode != NULL) 43 | { 44 | printf("%d\t", pNode->m_nValue); 45 | pNode = pNode->m_pNext; 46 | } 47 | 48 | printf("\nPrintList ends.\n"); 49 | } 50 | 51 | void DestroyList(ListNode* pHead) 52 | { 53 | ListNode* pNode = pHead; 54 | while(pNode != NULL) 55 | { 56 | pHead = pHead->m_pNext; 57 | delete pNode; 58 | pNode = pHead; 59 | } 60 | } 61 | 62 | void AddToTail(ListNode** pHead, int value) 63 | { 64 | ListNode* pNew = new ListNode(); 65 | pNew->m_nValue = value; 66 | pNew->m_pNext = NULL; 67 | 68 | if(*pHead == NULL) 69 | { 70 | *pHead = pNew; 71 | } 72 | else 73 | { 74 | ListNode* pNode = *pHead; 75 | while(pNode->m_pNext != NULL) 76 | pNode = pNode->m_pNext; 77 | 78 | pNode->m_pNext = pNew; 79 | } 80 | } 81 | 82 | void RemoveNode(ListNode** pHead, int value) 83 | { 84 | if(pHead == NULL || *pHead == NULL) 85 | return; 86 | 87 | ListNode* pToBeDeleted = NULL; 88 | if((*pHead)->m_nValue == value) 89 | { 90 | pToBeDeleted = *pHead; 91 | *pHead = (*pHead)->m_pNext; 92 | } 93 | else 94 | { 95 | ListNode* pNode = *pHead; 96 | while(pNode->m_pNext != NULL && pNode->m_pNext->m_nValue != value) 97 | pNode = pNode->m_pNext; 98 | 99 | if(pNode->m_pNext != NULL && pNode->m_pNext->m_nValue == value) 100 | { 101 | pToBeDeleted = pNode->m_pNext; 102 | pNode->m_pNext = pNode->m_pNext->m_pNext; 103 | } 104 | } 105 | 106 | if(pToBeDeleted != NULL) 107 | { 108 | delete pToBeDeleted; 109 | pToBeDeleted = NULL; 110 | } 111 | } -------------------------------------------------------------------------------- /051-数组中重复的数字/swap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | 21 | class Solution 22 | { 23 | public: 24 | // Parameters: 25 | // numbers: an array of integers 26 | // length: the length of array numbers 27 | // duplication: (Output) the duplicated number in the array number 28 | // Return value: true if the input is valid, and there are some duplications in the array number 29 | // otherwise false 30 | bool duplicate(int numbers[], int length, int* duplication) 31 | { 32 | *duplication = -1; 33 | 34 | if(CheckValidity(numbers, length) == false) 35 | { 36 | return false; 37 | } 38 | 39 | for(int i = 0; i < length; i++) 40 | { 41 | // 极端情况按顺序排序, 第i个位置上的数numbers[i]应该等于其下标i 42 | while(numbers[i] != i) 43 | { 44 | debug <<"numbers[" < max) 63 | { 64 | max = num; 65 | } 66 | if(num < min) 67 | { 68 | min = num; 69 | } 70 | debug <<"max = " < 2 | #include 3 | 4 | using namespace std; 5 | 6 | // 调试开关 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | class Solution 20 | { 21 | public: 22 | /*二分查找 找到第一个K 和 最后一个K 二者位置相减*/ 23 | int GetNumberOfK(vector data ,int k) 24 | { 25 | if(data.empty()) 26 | { 27 | return 0; 28 | } 29 | 30 | int number = 0; 31 | int first = GetFirstIndex(data, k, 0, data.size( ) - 1); 32 | int last = GetLastIndex(data, k, 0, data.size( ) - 1); 33 | 34 | if(first > -1 && last > -1) 35 | { 36 | number = last - first + 1; 37 | } 38 | return number; 39 | } 40 | 41 | /* 查找第一个的位置 */ 42 | int GetFirstIndex(vector &data, int k, int low, int high) 43 | { 44 | if(low > high) 45 | { 46 | return -1; 47 | } 48 | 49 | int mid = (low + high) / 2; 50 | 51 | if(data[mid] == k) 52 | { 53 | if((mid > 0 && data[mid-1] != k) || mid == 0) 54 | { 55 | return mid; 56 | } 57 | else 58 | { 59 | high = mid - 1; 60 | } 61 | } 62 | else if(data[mid] > k) 63 | { 64 | high = mid - 1; 65 | } 66 | else if(data[mid] < k) 67 | { 68 | low = mid + 1; 69 | } 70 | 71 | return GetFirstIndex(data, k, low, high); 72 | } 73 | 74 | int GetLastIndex(vector &data, int k, int low, int high) 75 | { 76 | if(low > high) 77 | { 78 | return -1; 79 | } 80 | 81 | int mid = (low + high) / 2; 82 | if(data[mid]==k) 83 | { 84 | if((mid < high && data[mid+1] != k) || mid == high) 85 | { 86 | return mid; 87 | } 88 | else 89 | { 90 | low = mid + 1; 91 | } 92 | } 93 | else 94 | { 95 | if(mid > k) 96 | { 97 | high = mid - 1; 98 | } 99 | else 100 | { 101 | low = mid + 1; 102 | } 103 | } 104 | 105 | return GetLastIndex(data, k, low, high); 106 | } 107 | }; 108 | 109 | 110 | 111 | int __tmain( ) 112 | { 113 | Solution solu; 114 | 115 | int arr[] = { 1, 2, 3, 3, 3, 3, 4, 5 }; 116 | vector vec(arr, arr + 8); 117 | cout < 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | 11 | // 调试开关 12 | #define __tmain main 13 | 14 | #ifdef __tmain 15 | 16 | #define debug cout 17 | 18 | #else 19 | 20 | #define debug 0 && cout 21 | 22 | #endif // __tmain 23 | 24 | class Solution 25 | { 26 | public : 27 | /* 方式二:利用队列来解决,时间复杂度为O(n) 28 | 利用双端队列来实现单调队列(索引对应的值是单调的) */ 29 | vector maxInWindows(const vector& num, unsigned int size) 30 | { 31 | unsigned int length = num.size( ); 32 | vector result; 33 | 34 | if(length == 0 || size == 0 || length < size) 35 | { 36 | return result; 37 | } 38 | 39 | deque indexQueue; 40 | 41 | /* 第一个窗口的处理比较简单, 直接找到最大的那个即可 */ 42 | for(unsigned int i = 0; 43 | i < size; 44 | i++) 45 | { 46 | /* 删除队尾元素 47 | * 对于当前元素num[i] 48 | * 前面比k小的,直接移出队列 49 | * 因为不再可能成为后面滑动窗口的最大值了 */ 50 | while(indexQueue.empty( ) != true 51 | && num[i] >= num[indexQueue.back( )]) 52 | { 53 | indexQueue.pop_back( ); 54 | } 55 | /* 将当前元素的下表压入队列中 */ 56 | indexQueue.push_back(i); 57 | } 58 | 59 | /* 处理后续的滑动窗口*/ 60 | for(unsigned int i = size; 61 | i < length; 62 | i++) 63 | { 64 | /* 队列中的第一个元素是当前滑动窗口最大值的下标 */ 65 | result.push_back(num[indexQueue.front()]); 66 | 67 | /* 删除队尾元素 68 | * 对于当前元素num[i] 69 | * 前面比k小的,直接移出队列 70 | * 因为不再可能成为后面滑动窗口的最大值了 */ 71 | while(indexQueue.empty( ) != true 72 | && num[i]>=num[indexQueue.back()]) 73 | { 74 | indexQueue.pop_back(); 75 | } 76 | 77 | /* 删除队首元素 78 | * 前面比k大的X, 79 | * 比较两者下标,判断X是否已不在窗口之内, 80 | * 不在了,直接移出队列 */ 81 | if(indexQueue.empty( ) != true 82 | && indexQueue.front( ) < (int)(i - size + 1)) 83 | { 84 | indexQueue.pop_front( ); 85 | } 86 | indexQueue.push_back(i); 87 | } 88 | result.push_back(num[indexQueue.front()]); 89 | return result; 90 | } 91 | }; 92 | 93 | int __tmain( ) 94 | { 95 | Solution solu; 96 | 97 | int array[] = { 2, 3, 4, 2, 6, 2, 5, 1 }; 98 | vector vec(array, array + 8); 99 | 100 | vector res = solu.maxInWindows(vec, 3); 101 | copy(res.begin( ), res.end( ), ostream_iterator(cout," ")); 102 | 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /036-数组中的逆序对/04-mergedivide.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | #define __tmain main 8 | 9 | #ifdef __tmain 10 | 11 | #define debug cout 12 | 13 | #else 14 | 15 | #define debug 0 && cout 16 | 17 | #endif // __tmain 18 | 19 | 20 | class Solution 21 | { 22 | public: 23 | int InversePairs(vector elem) 24 | { 25 | if(elem.size( ) == 0) 26 | { 27 | return 0; 28 | } 29 | 30 | vector temp(elem.size( )); 31 | int count = InversePairsCore(elem, 0, elem.size( ) - 1, temp); 32 | 33 | return count; 34 | } 35 | 36 | int MergeElem(vector &elem, int start, int mid, int end, vector &temp)//数组的归并操作 37 | { 38 | // int leftLen=mid-start+1; //elem[start...mid]左半段长度 39 | // int rightLlen=end-mid; //elem[mid+1...end]右半段长度 40 | 41 | int i = mid; 42 | int j = end; 43 | int k = 0; // 临时数组末尾坐标 44 | int count = 0; 45 | 46 | // 设定两个指针i, j分别指向两段有序数组的尾元素, 将小的那一个放入到临时数组中去. 47 | while(i >= start && j > mid) 48 | { 49 | if(elem[i] > elem[j]) 50 | { 51 | temp[k++] = elem[i--]; // 从临时数组的最后一个位置开始排序 52 | count += j - mid; // 因为elem[mid+1...j...end]是有序的,如果elem[i]>elem[j],那么也大于elem[j]之前的元素,从a[mid+1...j]一共有j-(mid+1)+1=j-mid 53 | } 54 | else 55 | { 56 | temp[k++] = elem[j--]; 57 | } 58 | } 59 | 60 | debug <<"count = " <= start)//表示前半段数组中还有元素未放入临时数组 62 | { 63 | temp[k++] = elem[i--]; 64 | } 65 | 66 | while(j > mid) 67 | { 68 | temp[k++] = elem[j--]; 69 | } 70 | 71 | //将临时数组中的元素写回到原数组当中去。 72 | for(i = 0; i < k; i++) 73 | { 74 | elem[end-i] = temp[i]; 75 | } 76 | 77 | return count; 78 | 79 | } 80 | 81 | int InversePairsCore(vector &elem,int start,int end, vector &temp) 82 | { 83 | int inversions = 0; 84 | if(start < end) 85 | { 86 | int mid = (start + end) / 2; 87 | inversions += InversePairsCore(elem, start, mid, temp); // 找左半段的逆序对数目 88 | inversions += InversePairsCore(elem, mid + 1, end, temp); // 找右半段的逆序对数目 89 | inversions += MergeElem(elem,start, mid, end, temp); // 在找完左右半段逆序对以后两段数组有序, 然后找两段之间的逆序对. 最小的逆序段只有一个元素. 90 | } 91 | 92 | return inversions; 93 | } 94 | }; 95 | 96 | int __tmain( ) 97 | { 98 | Solution solu; 99 | 100 | int arr[] = { 7, 5, 6, 4 }; 101 | vector vec(arr, arr + 4); 102 | 103 | cout < File Name: atoi.c 3 | > Author: GatieMe 4 | > Mail: gatieme@163.com 5 | > Created Time: 2016年04月02日 星期六 22时22分06秒 6 | ************************************************************************/ 7 | 8 | #include 9 | using namespace std; 10 | 11 | 12 | // 调试开关 13 | #define __tmain main 14 | 15 | #ifdef __tmain 16 | 17 | #define debug cout 18 | 19 | #else 20 | 21 | #define debug 0 && cout 22 | 23 | #endif // __tmain 24 | 25 | class Solution 26 | { 27 | public: 28 | int StrToInt(string str) 29 | { 30 | string::iterator pstr = str.begin( ); 31 | // 排除前导的空格 32 | while (*pstr == ' ') // 排除前导的空格 33 | { 34 | pstr++; 35 | } 36 | 37 | bool minus = false; 38 | 39 | // 判断符号位+ - 40 | if (*pstr == '+') 41 | { 42 | pstr++; 43 | } 44 | else if (*pstr == '-') 45 | { 46 | pstr++; 47 | minus = true; 48 | } 49 | 50 | 51 | long long int value = 0; 52 | for (; pstr != str.end( ); pstr++) 53 | { 54 | if ('0' <= *pstr && *pstr <= '9') 55 | { 56 | value *= 10; 57 | value += *pstr - '0'; 58 | debug <<"value = "< (unsigned long)(INT_MAX) + 1) // 负数绝对值最大为INT_MAX + 1 69 | || (minus == false && value > INT_MAX)) // 正数最大值为INT_MAX 70 | { 71 | debug <= INT_MAX) 90 | { 91 | value = INT_MAX; 92 | } 93 | else if (value <= INT_MIN) 94 | { 95 | value = INT_MIN; 96 | } 97 | 98 | return (int)value; 99 | 100 | } 101 | } 102 | }; 103 | 104 | 105 | int __tmain(void) 106 | { 107 | Solution solu; 108 | cout <<"INT_MAX" < 2 | #include 3 | 4 | 5 | using namespace std; 6 | 7 | 8 | // 调试开关 9 | #define __tmain main 10 | 11 | #ifdef __tmain 12 | 13 | #define debug cout 14 | 15 | #else 16 | 17 | #define debug 0 && cout 18 | 19 | #endif // __tmain 20 | 21 | 22 | 23 | /* 先用二分查找找出某个k出现的位置,然后再分别向前和向后查找总的个数*/ 24 | class Solution 25 | { 26 | 27 | public: 28 | int GetNumberOfK(vector data,int key) 29 | { 30 | if(data.size( ) == 0) 31 | { 32 | return 0; 33 | } 34 | 35 | // 用二分查找查找到Key的位置 36 | int index = BinarySearch(data, 0, data.size( ) - 1, key); 37 | //int index = BinarySearch(data, key); 38 | if(index == -1) 39 | { 40 | return 0; 41 | } 42 | 43 | int count = 1; 44 | // 查找前面部分Key的个数 45 | for(int j = index - 1; 46 | j >= 0 && data[j] == key; 47 | j--) 48 | { 49 | debug <<"pos = " < nstack; 52 | TreeNode *node = root; 53 | 54 | // 开始遍历整个二叉树 55 | while(node != NULL || nstack.empty() != true) 56 | { 57 | // 不输出当前根节点,但是递归直至当前根节点node的最左端 58 | while(node != NULL) 59 | { 60 | nstack.push(node); 61 | node = node->left; 62 | } 63 | 64 | // 此时栈顶的元素是当前最左元素 65 | // 它应该被输出 66 | if(nstack.empty( ) != true) 67 | { 68 | node = nstack.top( ); 69 | debug <val <right; 77 | } 78 | } 79 | return NULL; 80 | } 81 | }; 82 | 83 | 84 | int __tmain( ) 85 | { 86 | TreeNode tree[7]; 87 | 88 | tree[0].val = 5; 89 | tree[0].left = &tree[1]; 90 | tree[0].right = &tree[2]; 91 | 92 | tree[1].val = 3; 93 | tree[1].left = &tree[3]; 94 | tree[1].right = &tree[4]; 95 | 96 | tree[2].val = 7; 97 | tree[2].left = &tree[5]; 98 | tree[2].right = &tree[6]; 99 | 100 | tree[3].val = 2; 101 | tree[3].left = NULL; 102 | tree[3].right = NULL; 103 | 104 | tree[4].val = 4; 105 | tree[4].left = NULL; 106 | tree[4].right = NULL; 107 | 108 | tree[5].val = 6; 109 | tree[5].left = NULL; 110 | tree[5].right = NULL; 111 | 112 | tree[6].val = 8; 113 | tree[6].left = NULL; 114 | tree[6].right = NULL; 115 | 116 | Solution solu; 117 | TreeNode *res = NULL; 118 | if((res = solu.KthNode(tree, 3)) != NULL) 119 | { 120 | cout <val <) How do you check whether a string stands for a number or not? 4 | * Numbers include positive and negative integers and floats. For example, strings "+100.", "5e2", "-.123", "3.1416", and "-1E-16" 5 | * stand for numbers, but "12e", "1a3.14", "1.2.3", "+-5", and "12e+5.4" do not. 6 | */ 7 | 8 | #include 9 | 10 | bool scanDigits(char** str); 11 | bool isExponential(char** str); 12 | 13 | bool isNumeric(char* str) 14 | { 15 | if (str == NULL) 16 | return false; 17 | 18 | if (*str == '+' || *str == '-') 19 | ++str; 20 | if (*str == '\0') 21 | return false; 22 | 23 | bool numeric = true; 24 | 25 | bool hasDigits = scanDigits(&str); 26 | if (*str != '\0') 27 | { 28 | // for floats 29 | if (*str == '.') 30 | { 31 | ++str; 32 | if (*str == 'e' || *str == 'E') 33 | return false; 34 | if (!hasDigits && *str == '\0') 35 | return false; 36 | 37 | scanDigits(&str); 38 | 39 | if (*str == 'e' || *str == 'E') 40 | numeric = isExponential(&str); 41 | } 42 | // for integers 43 | else if (*str == 'e' || *str == 'E') 44 | numeric = isExponential(&str); 45 | else 46 | numeric = false; 47 | } 48 | 49 | return numeric && *str == '\0'; 50 | } 51 | 52 | bool scanDigits(char** str) 53 | { 54 | char* pBefore = *str; 55 | 56 | while (**str != '\0' && **str >= '0' && **str <= '9') 57 | ++(*str); 58 | 59 | // return true when there are some digits in str 60 | return *str > pBefore; 61 | } 62 | 63 | bool isExponential(char** str) 64 | { 65 | if (**str != 'e' && **str != 'E') 66 | return false; 67 | 68 | ++(*str); 69 | if (**str == '+' || **str == '-') 70 | ++(*str); 71 | 72 | if (**str == '\0') 73 | return false; 74 | 75 | scanDigits(str); 76 | return (**str == '\0') ? true : false; 77 | } 78 | 79 | // ==================== Test Code ==================== 80 | 81 | void Test(char* testName, char* str, bool expected) 82 | { 83 | if (testName != NULL) 84 | printf("%s begins: ", testName); 85 | 86 | if (isNumeric(str) == expected) 87 | printf("Passed.\n"); 88 | else 89 | printf("FAILED.\n"); 90 | } 91 | 92 | int main(int argc, char* argv[]) 93 | { 94 | Test("Test1", "100", true); 95 | Test("Test2", "123.45e+6", true); 96 | Test("Test3", "+500", true); 97 | Test("Test4", "5e2", true); 98 | Test("Test5", "3.1416", true); 99 | Test("Test6", "600.", true); 100 | Test("Test7", "-.123", true); 101 | Test("Test8", "-1E-16", true); 102 | Test("Test9", "1.79769313486232E+308", true); 103 | 104 | printf("\n\n"); 105 | 106 | Test("Test10", "12e", false); 107 | Test("Test11", "1a3.14", false); 108 | Test("Test12", "1+23", false); 109 | Test("Test13", "1.2.3", false); 110 | Test("Test14", "+-5", false); 111 | Test("Test15", "12e+5.4", false); 112 | Test("Test16", ".", false); 113 | Test("Test17", ".e1", false); 114 | Test("Test18", "+.", false); 115 | 116 | return 0; 117 | } 118 | -------------------------------------------------------------------------------- /ChineseCodingInterviewAppendix-master/RobotMove/RobotMove.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Question Description: 3 | * (Question 31 in ) A robot starts at cell (0, 0) of a grid with m rows and n columns. 4 | * It can move to the left, right, up, and down, and moves one cell for a step. It cannot enter cells where 5 | * the digit sum of the row index and column index are greater than a given k. 6 | */ 7 | 8 | #include 9 | 10 | int getDigitSum(int number) 11 | { 12 | int sum = 0; 13 | while(number > 0) 14 | { 15 | sum += number % 10; 16 | number /= 10; 17 | } 18 | 19 | return sum; 20 | } 21 | 22 | bool check(int threshold, int rows, int cols, int row, int col, bool* visited) 23 | { 24 | if(row >=0 && row < rows && col >= 0 && col < cols 25 | && getDigitSum(row) + getDigitSum(col) <= threshold 26 | && !visited[row* cols + col]) 27 | return true; 28 | 29 | return false; 30 | } 31 | 32 | int movingCountCore(int threshold, int rows, int cols, int row, int col, bool* visited) 33 | { 34 | int count = 0; 35 | if(check(threshold, rows, cols, row, col, visited)) 36 | { 37 | visited[row * cols + col] = true; 38 | 39 | count = 1 + movingCountCore(threshold, rows, cols, 40 | row - 1, col, visited) 41 | + movingCountCore(threshold, rows, cols, 42 | row, col - 1, visited) 43 | + movingCountCore(threshold, rows, cols, 44 | row + 1, col, visited) 45 | + movingCountCore(threshold, rows, cols, 46 | row, col + 1, visited); 47 | } 48 | 49 | return count; 50 | } 51 | 52 | int movingCount(int threshold, int rows, int cols) 53 | { 54 | bool *visited = new bool[rows * cols]; 55 | for(int i = 0; i < rows * cols; ++i) 56 | visited[i] = false; 57 | 58 | int count = movingCountCore(threshold, rows, cols, 59 | 0, 0, visited); 60 | 61 | delete[] visited; 62 | 63 | return count; 64 | } 65 | 66 | // ================================ test code ================================ 67 | void test(char* testName, int threshold, int rows, int cols, int expected) 68 | { 69 | if(testName != NULL) 70 | printf("%s begins: ", testName); 71 | 72 | if(movingCount(threshold, rows, cols) == expected) 73 | printf("Passed.\n"); 74 | else 75 | printf("FAILED.\n"); 76 | } 77 | 78 | void test1() 79 | { 80 | test("Test1", 5, 10, 10, 21); 81 | } 82 | 83 | void test2() 84 | { 85 | test("Test2", 15, 20, 20, 359); 86 | } 87 | 88 | void test3() 89 | { 90 | test("Test3", 10, 1, 100, 29); 91 | } 92 | 93 | void test4() 94 | { 95 | test("Test4", 10, 1, 10, 10); 96 | } 97 | 98 | void test5() 99 | { 100 | test("Test5", 15, 100, 1, 79); 101 | } 102 | 103 | void test6() 104 | { 105 | test("Test6", 15, 10, 1, 10); 106 | } 107 | 108 | void test7() 109 | { 110 | test("Test7", 15, 1, 1, 1); 111 | } 112 | 113 | void test8() 114 | { 115 | test("Test8", -10, 10, 10, 0); 116 | } 117 | 118 | int main(int agrc, char* argv[]) 119 | { 120 | test1(); 121 | test2(); 122 | test3(); 123 | test4(); 124 | test5(); 125 | test6(); 126 | test7(); 127 | test8(); 128 | } -------------------------------------------------------------------------------- /038-数字在排序数组中出现的次数/03-firstlast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | using namespace std; 6 | 7 | 8 | // 调试开关 9 | #define __tmain main 10 | 11 | #ifdef __tmain 12 | 13 | #define debug cout 14 | 15 | #else 16 | 17 | #define debug 0 && cout 18 | 19 | #endif // __tmain 20 | 21 | class Solution 22 | { 23 | public: 24 | int GetNumberOfK(std::vector array, int k) 25 | { 26 | if (array.size( ) == 0) 27 | { 28 | return 0; 29 | } 30 | 31 | int low = 0, high = array.size( ) - 1; 32 | int first = 0, last = 0; 33 | int number = 0; 34 | 35 | if (low == high) 36 | { 37 | if (array[0] != k) 38 | { 39 | return 0; 40 | } 41 | else 42 | { 43 | return 1; 44 | } 45 | } 46 | 47 | first = GetFirstIndex(array, 0, array.size( ) - 1, k); 48 | last = GetLastIndex(array, 0, array.size( ) - 1, k); 49 | 50 | if(first > -1 && last > -1) 51 | { 52 | debug <<"first = " <