实例介绍
LeetCode cpp最新中文题解.pdfLeetCode cpp最新中文题解.pdfLeetCode cpp最新中文题解.pdf
目录 3.4 Add binary 61 5.1.5 Binary Tree Level Or- 3.5 Longest Palindromic Substring. 62 der traversal il 3.6 Regular Expression Matching 66 5.1.6 Binary Tree Zigzag 3.7 Wildcard Matching 67 Level Order traversal. 96 3.8 Longest Common Prefix 5.1.7 Recover Binary Search 3. 9 Valid Number 70 Tree 98 3.10 Integer to roman 72 5. 1. 8 Same Tree 3. 11 Roman to Integer 73 5.1.9 Symmetric Tree 100 3.12 Count and Say 74 5.1.10 Balanced Binary Tree.. 102 3. 13 Anagrams 75 5.1.11 Flatten Binary Tree to 3. 14 Simplify Path 76 Linked List 103 3. 15 Length of Last Word 77 5.1. 12 Populating Next Right Pointers in each node ii 105 第4章栈和队列 7952二叉树的构建 106 41栈 79 5.2.1 Construct Binary Tree 4 Valid Parentheses 79 from Preorder and In 4.1.2 Longest valid Paren order Traversa 106 theses 80 5.2.2 Construct Binary Tree 4.1.3 Largest Rectangle in from Inorder and pos Histogram 82 torder Traversal 107 4.1.4 Evaluate reverse pol- 53二叉查找树 108 ish notation 84 5.3. 1 Unique Binary Search 4.2队列 85 Trees .108 5.3.2 Unique Binary Search 第5章树 86 Trees li .110 5.1二叉树的遍历 86 5.3.3 Validate Binary Search 5.1.1 Binary Tree Preorder Tree Traversal 86 5.3. 4 Convert Sorted array to 5.1.2 Binary Tree Inorder Binary Search Tree...112 Traversal 88 5.3.5 Convert Sorted List to 5.1. 3 Binary Tree Postorder Binary Search Tree 113 Traversal 90 54二叉树的递归 .114 5. 1. 4 Binary Tree Level Or 5.4.1 Minimum Depth of Bi der traversal )2 nary lree 115 目录 5.4.2 Maximum Depth of Bi 8.3.,2重新实现 next permu nary Tree 116 tation 142 5.4.3 Path Sum 117 83.3递归 .143 5.4 4 Path Sum il 118 8.4 Permutations II 144 5.4.5 Binary Tree Maximum 8.4.1 next permutation... 144 Path Su um 119 84.2重新实现 next permu 5.4.6 Populating Next Right tation 144 Pointers in each node 120 843递归 144 5.4.7 Sum Root to Leaf num 8.5 Combinations 146 bers 21 85.1递归 146 8.5.2迭代 147 第6章排序 123 8.6 Letter Combinations of a phone 6.1 Merge Sorted Array 123 umber 147 6.2 Merge Two Sorted Lists 124 86.1递归 148 6.3 Merge k Sorted Lists 124 862迭代 9 6.4 Insertion Sort List 125 第9章广度优先搜索 150 6.5 Sort list 126 9.1 Word Ladder 150 6.6 First Missing Positive 127 9.2 Word Ladder il 154 6.7 Sort Colors 128 9.3 Surrounded regions 162 第7章查找 94小结 164 131 94.l适用场景 164 7.1 Search for a range 131 942思考的步骤 7.2 Search Insert Position .132 94.3代码模板 165 7. 3 Search a 2D Matrix 133 第10章深度优先搜索 173 第8章暴力枚举法 135 10.1 Palindrome Partitioning 173 8.1 Subsets 135 10.2 Unique Paths 176 8.1.1递归 135 0.2.1深搜 176 8.1.2迭代 .137 1022备忘录法 .176 8.2 Subsets il 138 10.23动规 177 821递归 138 10.24数学公式 178 8.2.2迭代 .141 10.3 Unique Paths Il 179 8. 3 Permutations 142 10.3.1备忘录法 179 8.3.1 next permutation 142 10.3.2动规 .180 目录 10.4 N-Queens 18 13.4 Maximal rectangle 213 10.5 N-Queens II 184 13.5 Best Time to Buy and Sell Stock 10.6 Restore ip addresses 186 .214 10.7 Combination Sum 188 13.6 Interleaving String 215 10.8 Combination Sum Il 189 13.7 Scramble String 217 10.9 Generate Parentheses .190 13. 8 Minimum Path Sum .222 10.10 Sudoku solver 192 13.9 Edit Distance 224 10.11 Word Search .193 13. 10 Decode Ways .226 10.12小结 195 13. 11 Distinct sub 227 10.12.1适用场景 195 13. 12 Word Break 228 10.122思考的步骤 195 13 13 Word Break il 230 0.12.3代码模板 197 第14章图 232 10.12.4深搜与回溯法的区別.197 14. 1 Clone Graph 232 10.12.5深搜与递归的区别..197 第15章细节实现题 235 第11章分治法 199 15.1 Reverse Integer 235 1.1 Pow(x, n) 199 15.2 Palindrome Number .236 11. 2 Sqrt(x) 200 15.3 Insert Interval 237 第12章贪心法 201 15.4 Merge Intervals 238 12.1 Jump game 201 15.5 Minimum Window Substring 239 12.2 Jump game II 15.6 Multiply Strings 241 12. 3 Best Time to buy and Sell stock 204 15.7 Substring with Concatenation 12. 4 Best Time to buy and sell stock l205 of all words 244 12. 5 Longest Substring Without re 15.8 Pascal,s Triangle 245 peating Characters 206 15.9 Pascals Triangle Il 246 12.6 Container with Most Water.. 207 15.10 Spiral Matrix 247 15.11 Spiral matrix II 248 第13章动态规划 209 15.12 ZigZag Conversion 250 13. 1 Triangle 209 15.13 Divide Two Integers 251 13.2 Maximum Subarray 15. 14 Text Justification 253 13.3 Palindrome Partitioning II 12 15.15 Max Points on a line 255 目录 第1章 编程技巧 在判断两个浮点数a和b是否相等时,不要用a==b,应该判断二者之差的绝对值 fabs(a-b)是否小于某个阈值,例如1e-9。 判断一个整数是否是为奇数,用x%2!=0,不要用x%2=1,因为ⅹ可能是负 用char的值作为数组下标(例如,统计字符串中每个字符岀现的次数),要考虑到 char可能是负数。有的人考虑到了,先强制转型为 unsigned int再用作下标,这仍然是 错的。正确的做法是,先强制转型为 unsigned char,再用作下标。这涉及C十整型提升 的规则,就不详述了。 以下是关于STL使用技巧的,很多条款来自《 Effective STL》这本书。 vector和 string优先于动态分配的数组 首先,在性能上,由于 vector能够保证连续内存,因此一旦分配了后,它的性能跟 原始数组相当; 其次,如果用new,意味着你要确保后面进行孓 delete,一旦忘记了,就会出现BUG, 且这样需要都写一行 delete,代码不够短 再次,声明多维数组的话,只能一个一个new,例如 int** ary = new int*[row_num]; for(int i=0: i< row num; ++1) ary [i] new int [col_num] 用 vector的话一行代码搞定 vector<vector<int>>ary(row_num, vector<int>(col_num, 0)) 使用 reserve来避免不必要的重新分配 第2章 线性表 这类题目考察线性表的操作,例如,数组,单链表,双向链表等。 2数组 2.1.1 Remove Duplicates from Sorted array 描述 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length Do not allocate extra space for another array, you must do this in place with constant memory For example, Given input array A =[1, 1, 2 Your function should return length =2, and a is now [1, 2] 分析 无 代码1 / LeetCode, Remove Duplicates from Sorted Array //时间复杂度0(n),空间复杂度0(1) class Solution t ublic int removeDuplicates(vector<int>& nums)t if (nums empty o) return 0; int index =o for (int i =1: i nums size: 1++ i if (nums [index] ! nums [i]) nums [++index]= nums [i] return index 1 2.1数组 代码2 //LeetCode, Remove Duplicates from Sorted Array /使用STL,时间复杂度0(n),空间复杂度0(1) class Solution i public int removeDuplicates(vector<int>& nums)t return distance(nums begin(), unique(nums begin(), nums end ()) 代码3 / LeetCode, Remove Duplicates from Sorted Array /使用STL,时间复杂度0(n),空间复杂度0(1) lass Solution f ublic int removeDuplicates(vector<int>& nums)t return distance(nums begin(, removeDuplicates(nums begin(, nums end(), nums begin template<typename InIt, typename outit> OutIt removeDuplicates(InIt first, InIt last, OutIt output)t hile (first last)i *output++ = *first first upper_bound(first, last, *first return output 相关题目 Remove Duplicates from Sorted Array I,见§2.1.2 2.1.2 Remove Duplicates from Sorted Array II 描述 Follow up for"Remove Duplicates " What if duplicates are allowed at most twice? For example, Given sorted array a =[1, 1, 1, 2, 2, 3] Your function should return length=5, and A is now [1, 1, 2, 2, 3 分析 加一个变量记录一下元素出现的次数即可。这题因为是已经排序的数组,所以一个变量即可解 决。如果是没有排序的数组,则需要引入一个 hashmap来记录出现次数。 4 第2章线性表 代码1 // Leet Code, Remove Duplicates from Sorted Array II //时间复杂度0(n),空间复杂度0(1) //qauthorhex108(https://github.com/hex108) class Solution t ublic int removeDuplicates(vector<int>& nums)t if (nums size(<= 2) return nums size int index =2 for (int i=2: i nums size(: i++)f f (nums [i] ! nums lindex -2] nums [index++]= nums [i] return index; 代码2 下面是一个更简洁的版本。上面的代码略长,不过扩展性好一些,例如将 occur<2改为 occur 3,就变成了允许重复最多3次。 // Leet Code, Remove Duplicates from Sorted Array II 7/@author虞航仲(http://weibo.com/u/1666779725) //时间复杂度0(n),空间复杂度0(1) lass Solution f public int removeDuplicates(vector<int>& nums)t const int n=nums size int index =0: for (int i=0:i <n; ++i)i f (i>0&&i<n-1 & nums [i] = nums [i -1] & nums [i] = nums [i 1]) continue; nums [index++]= nums [i] return index; 相关题目 Remove Duplicates from Sorted Array,见§2.1.1 【实例截图】
【核心代码】
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论