# leetcode **Repository Path**: nateshao1024/leetcode ## Basic Information - **Project Name**: leetcode - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-04-25 - **Last Updated**: 2026-04-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README [TOC] ![](https://repobeats.axiom.co/api/embed/5d41120dc0ef13265fcde9fccd622d275999e4b9.svg "Repobeats analytics image") # leetcode leetcode 代码成长记录 学习数据结构与算法的代码示例,目前提供 Java语言支持。编程是一门实践的手艺,多多练习,多多思考,把这里列举的所有算法,数据结构,以及对应的常见 leetcode 习题都自己手敲几遍,增强自己的编码基本功,写出高性能和高质量的代码! ## 十大经典排序算法 文章介绍看这里 [十大经典排序算法](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247485793&idx=1&sn=426347202b22e700b2aab172de0aca98&chksm=e8744051df03c9476d218cf9ea685cb71611b9bdce5562a26c898a7023b25034346768875ff6&token=2138841491&lang=zh_CN#rd) | 序号 | 排序名称 | 代码实现 | | :--: | :------: | :----------------------------------------------------------: | | 1 | 冒泡排序 | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/basic_01_ten_sort/Code_01_BubbleSort.java) | | 2 | 插入排序 | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/basic_01_ten_sort/Code_02_InsertionSort.java) | | 3 | 希尔排序 | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/basic_01_ten_sort/Code_03_ShellSort.java) | | 4 | 选择排序 | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/basic_01_ten_sort/Code_04_SelectionSort.java) | | 5 | 快速排序 | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/basic_01_ten_sort/Code_05_QuickSort.java) | | 6 | 归并排序 | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/basic_01_ten_sort/Code_06_MergeSort.java) | | 7 | 计数排序 | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/basic_01_ten_sort/Code_07_CountSort.java) | | 8 | 基数排序 | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/basic_01_ten_sort/Code_08_RadixSort.java) | | 9 | 桶排序 | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/basic_01_ten_sort/Code_09_BucketSort.java) | | 10 | 堆排序 | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/basic_01_ten_sort/Code_10_HeapSort.java) | ## 《剑指offer》题目 | 序号 | 题目 | 题解 | 代码实现 | 难度 | | ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | | 1 | 面试题1:赋值运算符函数 | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486270&idx=1&sn=f412b72e576dad485618decd4b74e296&chksm=e874420edf03cb1864f88c0ae735c7d3631273969c4dce3968279d4e269ecc53c951d396adbd&token=120873784&lang=zh_CN#rd) | | | | 2 | 面试题2:实现Singleton模式 | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486284&idx=1&sn=7f0e4099fb39576876890c3c999cc58a&chksm=e874427cdf03cb6a57a9538a01a84230db008a88cdb9ae042cbb2cd9fbacd671a4968c15b51b&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_01_Singleton/Singleton.java) | | | 3 | [剑指 Offer 03. 替换空格](https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486301&idx=1&sn=0e703bb09c52f6e622c0eb04d123e7ed&chksm=e874426ddf03cb7b9d8e268b6ee22d612be0984047fd8985f27238f2d482891bff4dbc1faed4&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_04_replaceSpace/Solution.java) | 简单 | | 4 | [剑指 Offer 04. 二维数组中的查找](https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486331&idx=1&sn=f99f800fe53b696b7170a05311d25a52&chksm=e874424bdf03cb5d4873beb8fdb0bc389d016efc5c87306ea65cf1c9e77aa29b9158235b8dc1&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_02_find_array/FindArray.java) | 中等 | | 5 | [剑指 Offer 05. 从尾到头打印链表](https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486343&idx=1&sn=68395e0c0d6a14468eca1317d702d8e6&chksm=e87442b7df03cba1b17c2acfbc8c3284a0431c030e9a550c676e641ded8629baedddd3900a7b&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_05_reversePrint/Solution.java) | 简单 | | 6 | [剑指 Offer 06. 重建二叉树](https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486397&idx=1&sn=3cf1e1afc74a03bbbbc1e49b6fad844d&chksm=e874428ddf03cb9bc72bc410e3455d70877f20cb44be9dd18107d9c656e734ab25009f5d24e9&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_06_buildTree/Solution.java) | 中等 | | 7 | [剑指 Offer 07. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486416&idx=1&sn=1c6a67f6176fd2ad1e6d345f41edb3d5&chksm=e87442e0df03cbf6cb5fd0d9e8a07328e9fe9e38507f54409e84870c66d8feac30197b81179e&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_07_CQueue/CQueue.java) | 简单 | | 8 | [剑指 Offer 08. 旋转数组的最小数字](https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486450&idx=1&sn=d4b28aafc89f2ff3f1c9ea9d71dffe6d&chksm=e87442c2df03cbd49c32904b0fbe3c22c3258168158a275f2b886694e4f63e0810e45f922549&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_08_minArray/Solution.java) | 简单 | | 9 | [剑指 Offer 09. 斐波那契数列](https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof/) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486534&idx=2&sn=7d5167c8a385931ed2bbc2f8478f549c&chksm=e8744576df03cc608bab8d7837334c0c49b837a71e9063147cb95f5c6ffb909af597256a42d3&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_09_fib/Solution.java) | 简单 | | 10 | [剑指 Offer 10- II. 青蛙跳台阶问题](https://leetcode-cn.com/problems/qing-wa-tiao-tai-jie-wen-ti-lcof/) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486534&idx=1&sn=a9040533e73e3274d13ef93283a08793&chksm=e8744576df03cc602d5d29370ab20512bc048e155c9d8973b94cbbce0aa29a003a593f3a858b&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_10_numWays/Solution.java) | | | 11 | [剑指 Offer 11. 矩形覆盖](https://www.nowcoder.com/practice/72a5a919508a4251859fb2cfb987a0e6?tpId=13&tqId=11163&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486597&idx=2&sn=3d97a48cfbfb9c9ab19090c7102815a0&chksm=e87445b5df03cca354cfc84d23c265a6e5e2aa760f22fb6d616c0781f87431bb4901ad1150f6&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_11_RectCover/Solution.java) | | | 12 | [剑指 Offer 12. 二进制中1的个数](https://www.nowcoder.com/practice/72a5a919508a4251859fb2cfb987a0e6?tpId=13&tqId=11163&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486597&idx=1&sn=539b8711d7fe00d3ad820eb9add75375&chksm=e87445b5df03cca34e5605592e10f18750ced314590bae7b5105a701f08897866ecbaaa9e245&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_12_hammingWeight/Solution.java) | | | 13 | [剑指 Offer 13. 数值的整数方](https://leetcode-cn.com/problems/shu-zhi-de-zheng-shu-ci-fang-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486636&idx=1&sn=e9c819df042635c28114afeec4e34a08&chksm=e874459cdf03cc8a4ae1f1c3f92bc0e4da6b5c56ff748183a1be8dc248674077f9575be4d911&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_13_myPow/Solution.java) | | | 14 | [剑指 Offer 14. 打印从1到最大的n位数](https://leetcode-cn.com/problems/da-yin-cong-1dao-zui-da-de-nwei-shu-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486662&idx=1&sn=8f5c234616aac9bc7d81ede2dcd3dd5b&chksm=e87445f6df03cce0b6b485d0dd9ec98b94bf7ba1cd40280e440bdf6db07f23a281be6aa78ca3&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_12_hammingWeight/Solution.java) | 中等 | | 15 | [剑指 Offer 15. 删除链表的节点](https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486751&idx=1&sn=384d6623dd133e25cd2ff6996bf5abb8&chksm=e874442fdf03cd39f63c7fa3b8a0477bd217a30e0c395c0f552b21f6e84efd59919d81a0f616&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_12_hammingWeight/Solution.java) | | | 16 | [剑指 Offer 16. 将数组中的奇数放在偶数前](https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486751&idx=2&sn=d233627d16fcab570b837d86222a29ca&chksm=e874442fdf03cd3919f12f4b43cabcf64aa6d708594d9674b9685f5c4cf50d2a845af5ebd4d7&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_16_exchange/Solution.java) | 简单 | | 17 | [剑指 Offer 17. 链表中倒数第k个节点](https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486751&idx=3&sn=0092508d153af2eebd5195200572d825&chksm=e874442fdf03cd39bd0cba8beeaf066b4ae53fc3b0281339a4a68635236860efc4550be21684&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_17_getKthFromEnd/Solution.java) | | | 18 | [剑指 Offer 18. 反转链表](https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486751&idx=4&sn=7b3072ff351b4a94074a8585a09d089b&chksm=e874442fdf03cd39b8c2f1fe6d8d6eda9ae0927988758bd066f95bef3da0f294756dde907b6c&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_18_reverseList/Solution.java) | 简单 | | 19 | [剑指 Offer 19. 合并两个有序链表](https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486751&idx=5&sn=13af47bed49c227c525480c32ba844de&chksm=e874442fdf03cd39aaef0dc808891fb1f41a900b863b632f9299b007fb826d9010473326e3b4&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_19_mergeTwoLists/Solution.java) | 简单 | | 20 | [剑指 Offer 20. 判断二叉树A中是否包含子树B](https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486768&idx=1&sn=9360a07ba7135ebdae977e6ddaeab891&chksm=e8744400df03cd1668da38471fe2bcfbfa762a8f9fff16ef9e88c5f74bde16a8a16698ff02a0&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_20_isSubStructure/Solution.java) | | | 21 | [剑指 Offer 21. 二叉树的镜像](https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486805&idx=1&sn=f934a76e18adfbfc67f9e7bb8c256d22&chksm=e8744465df03cd7323f25df7c622050c6bd754feb986294e79b2528b2340f0662798228b9b59&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_21_mirrorTree/Solution.java) | | | 22 | [剑指 Offer 22. 顺时针打印矩阵](https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486805&idx=1&sn=f934a76e18adfbfc67f9e7bb8c256d22&chksm=e8744465df03cd7323f25df7c622050c6bd754feb986294e79b2528b2340f0662798228b9b59&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_22_spiralOrder/Solution.java) | | | 23 | [剑指 Offer 23. 包含min函数的栈](https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486819&idx=1&sn=90b7556cf33b743768ba7c873cea078f&chksm=e8744453df03cd45933207f3418bff81be36d00d8831066a30520691f88fd7450b675fd3418a&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_23_MinStack/MinStack.java) | | | 24 | [剑指 Offer 24. 栈的压入-弹出序列](https://leetcode-cn.com/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247486856&idx=1&sn=04aacae5c923316df412024ef4c2a290&chksm=e87444b8df03cdaee1becd8f60999b04cd3ce08a463d8ff834f5104d8f52ed523c745374c9e4&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_24_validateStackSequences/Solution.java) | | | 25 | [剑指 Offer 25. 从上到下打印二叉树](https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247487022&idx=1&sn=7c0d5dd4f49005b17dcbe13955f6f857&chksm=e874471edf03ce08024d87d0f1ed3191e1348f082f6d5e5e3fcc19c05d687be1d851153e4745&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_25_levelOrder/Solution.java) | | | 26 | [剑指 Offer 26. 二叉搜索树的后序遍历序列](https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247487045&idx=1&sn=d4c8bcf90d84ee120a7f91307d85f7d2&chksm=e8744775df03ce63e875a1ba3889e611a75fd8659cffd1bb7ca995c3ed2fdc539d200d0b39c2&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_25_levelOrder/Solution.java) | | | 27 | [剑指 Offer 27. 二叉树中和为某一值的路径](https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof) | [点我](https://mp.weixin.qq.com/s?__biz=MzIyNjE0MDI1NQ==&mid=2247487078&idx=1&sn=f58fb094364f7259966b7547e28f54cc&chksm=e8744756df03ce4037a714c849025569dd3b2254b0a9f0eaea4f2ac10e69fc15d57023424b38&token=120873784&lang=zh_CN#rd) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_27_pathSum/Solution.java) | 中等 | | 28 | [剑指 Offer 28. 复杂链表的复制](https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof) | | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_28_copyRandomList/Solution.java) | 中等 | | 29 | [剑指 Offer 29. 二叉搜索树转换为双向链表](https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/) | [点我]() | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_29_treeToDoublyList/Solution.java) | | | 30 | [剑指 Offer 30. 字符串的排列](https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/) | [点我]() | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_30_permutation/Solution.java) | | | 31 | [剑指 Offer 31. 数组中出现次数超过一半的数字](https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/) | [点我]() | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_31_majorityElement/Solution.java) | | | 32 | [剑指 Offer 32. 最小的k个数](https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/) | [点我]() | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_32_getLeastNumbers/Solution.java) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ## 二叉树 二叉树解题的思维模式分两类: 1. **是否可以通过遍历一遍二叉树得到答案?**如果可以,用一个 traverse 函数配合外部变量来实现,这叫「遍历」的思维模式。 2. **是否可以定义一个递归函数,通过子问题(子树)的答案推导出原问题的答案?**如果可以,写出这个递归函数的定义,并充分利用这个函数的返回值,这叫「分解问题」的思维模式。 无论使用哪种思维模式,你都需要思考: 如果单独抽出一个二叉树节点,它需要做什么事情?需要在什么时候(前/中/后序位置)做?其他的节点不用你操心,递归函数会帮你在所有节点上执行相同的操作。 | 序号 | 题目 | 解题代码 | 题目难度 | 备注 | | :--: | ------------------------------------------------------------ | :----------------------------------------------------------: | :------: | ---- | | 1 | [144. 二叉树的前序遍历](https://leetcode-cn.com/problems/binary-tree-preorder-traversal/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code144_preorderTraversal/Solution.java),[Go](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/go/leetcode/binary_tree/code01_144_preorderTraversal/main.go) | 简单 | | | 2 | [94. 二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code94_inorderTraversal/Solution.java),[Go](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/go/leetcode/binary_tree/code02_94_inorderTraversal/main.go) | 简单 | | | 3 | [145. 二叉树的后序遍历](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code145_postorderTraversal/Solution.java),[Go](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/go/leetcode/binary_tree/code03_145_postorderTraversal/main.go) | 简单 | | | 4 | [104. 二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code04_104_maxDepth/Solution.java),[Go](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/go/leetcode/binary_tree/code04_104_maxDepth/main.go) | 简单 | | | 5 | [543. 二叉树的直径](https://leetcode-cn.com/problems/diameter-of-binary-tree/) | Java,Go | 简单 | | | 6 | [102. 二叉树的层序遍历](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/) | Java,Go | 简单 | | | | | | | | | 7 | [226. 翻转二叉树](https://leetcode-cn.com/problems/invert-binary-tree/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code07_226_linvertTree/Solution.java),Go | 简单 | | | 8 | [116. 填充每个节点的下一个右侧节点指针](https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code08_116_connect/Solution.java),Go | 简单 | | | 9 | [114. 二叉树展开为链表](https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code09_114_flatten/Solution.java),Go | 中等 | | | | | | | | | 10 | [654. 最大二叉树](https://leetcode-cn.com/problems/maximum-binary-tree/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code10_645_constructMaximumBinaryTree/Solution.java),Go | | | | 11 | [105. 从前序与中序遍历序列构造二叉树](https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code11_105_buildTree/Solution.java),Go | | | | 12 | [106. 从中序与后序遍历序列构造二叉树](https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code12_106_buildTree/Solution.java),Go | | | | 13 | [889. 根据前序和后序遍历构造二叉树](https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/) | [Java](https://gitee.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code13_889_constructFromPrePost/Solution.java),Go | | | | | | | | | | 14 | [297. 二叉树的序列化与反序列化](https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/) | [Java](https://gitee.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code14_297_Codec/Codec.java),Go | | | | 15 | [652. 寻找重复的子树](https://leetcode-cn.com/problems/find-duplicate-subtrees/) | [Java](https://gitee.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code15_652_findDuplicateSubtrees/Solution.java),Go | | | | | 归并排序详解及应用 | | | | | 16 | [912. 排序数组](https://leetcode-cn.com/problems/sort-an-array/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code16_912_sortArray/Solution.java),Go | 中等 | | | 17 | [315. 计算右侧小于当前元素的个数](https://leetcode-cn.com/problems/count-of-smaller-numbers-after-self/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code17_315_countSmaller/Solution.java),Go | 困难 | | | 18 | [493. 翻转对](https://leetcode-cn.com/problems/reverse-pairs/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code18_439_reversePairs/Solution.java),Go | 困难 | | | 19 | [327. 区间和的个数](https://leetcode-cn.com/problems/count-of-range-sum/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code19_327_countRangeSum/Solution.java),Go | 困难 | | | | 中序遍历解决升序问题 | | | | | 20 | [230. 二叉搜索树中第K小的元素](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/) | [Java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code20_230_kthSmallest/Solution.java),Go | 中等 | | | 21 | [538. 把二叉搜索树转换为累加树](https://leetcode-cn.com/problems/convert-bst-to-greater-tree/) | [Java](https://gitee.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/binary_tree/code21_538_convertBST/Solution.java),Go | 中等 | | | | | Java,Go | | | ## 数组 - 常见的leetcode习题: | 题号 | 题目名称 | 解题代码 | 难度 | | :--: | :------------------: | :----------------------------------------------------------: | :--: | | 26 | 删除有序数组的重复项 | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/Arrays/MaxProfit_122.java) | 简单 | | 122 | 买卖股票的最佳时机 | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/leetcode/Arrays/Remove_Repeat_Array_26.java) | 中等 | | | | | | ## 栈 - 常见的leetcode习题: | 题号 | 题目名称 | 解题代码 | 难度 | | :--: | :----------------------------------------------------------: | :----------------------------------------------------------: | :--: | | 20 | [有效的括号](https://leetcode-cn.com/problems/valid-parentheses/) | [java](https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/play_with_data_structures/lesson_02_stacks_and_queues/stack_leetcode/IsValid.java) | 简单 | | | | | | | | | | | ## 打卡第二轮《剑指offer》68题 1天4题=17天 | 天数 | 第一题 | 第二题 | 第三题 | 第四题 | 备注 | | :---------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :--------: | | 第一天-2022-03-29 | [剑指 Offer 03. 数组中重复的数字](https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/) | [剑指 Offer 04. 二维数组中的查找](https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/) | [剑指 Offer 05. 替换空格](https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/) | [剑指 Offer 06. 从尾到头打印链表](https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/) | | | 思路 | 解法1:hashSet遍历数组,有相同的直接返回,将不重复的添加到数组
解法2:先排序,遍历数组,如果前一位等于后一位,说明重复,直接返回 | 二分法
从矩阵 matrix 左下角元素(索引设为 (i, j) )开始遍历,并与目标值对比: 当 matrix[i] [j] > target 时,执行 i-- ,即消去第 i 行元素; 当 matrix[i][j] < target 时,执行 j++ ,即消去第 j 列元素; 当 matrix[i] [j] = target 时,返回 true ,代表找到目标值。 若行索引或列索引越界,则代表矩阵中无目标值,返回 false 。 | 初始化一个 StringBuilder,记为 res ; 遍历列表 s 中的每个字符 c : 当 c 为空格时:向 res 后添加字符串 "%20" ; 当 c 不为空格时:向 res 后添加字符 c ;将列表 res 转化为字符串并返回。 | 入栈: 遍历链表,将各节点值 push 入栈。(Python 使用 append() 方法,Java借助 LinkedList 的addLast()方法)。 * 出栈: 将各节点值 pop 出栈,存储于数组并返回。(Python 直接返回 stack 的倒序列表, * Java 新建一个数组,通过 popLast() 方法将各元素存入数组,实现倒序输出)。 | | | 第二天2022-03-30 | [剑指 Offer 07. 重建二叉树](https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof/) | [剑指 Offer 09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/) | [剑指 Offer 10- I. 斐波那契数列](https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof/) | [剑指 Offer 10- II. 青蛙跳台阶问题](https://leetcode-cn.com/problems/qing-wa-tiao-tai-jie-wen-ti-lcof/) | | | 思路 | 构造二叉树,第一件事一定是找根节点,然后想办法构造左右子树。 前序遍历结果第一个就是根节点的值,然后再根据中序遍历结果确定左右子树的节点。 | ![](https://pic.leetcode-cn.com/966aebd484002e620d88676847273a061ab9ab6d863ab5079ab347a643461e24-09.gif)初始化两个链表, 栈 B 元素实现栈 A 元素倒序 ,然后通过B.removeLast()将元素移出去。栈A作为辅助元素 | 1,**动态规划** `public static int fib(int n) { int a = 0, b = 1, sum; for (int i = 0; i < n; i++) { sum = (a + b) % 1000000007; a = b; b = sum; } return a; }`。2,**递归** `public static int fib2(int n) { n = help(n); return n; } private static int help(int n) { if (n < 2) return n; return (help(n - 1) + help(n - 2)) % 1000000007; }` | 1. 动态规划 `public static int numWays(int n) { int a = 0, b = 1, sum; for (int i = 0; i < n; i++) { sum = (a + b) % 1000000007; a = b; b = sum; } return a; }` | 重建二叉树 | | 第三天2022-03-31 | [剑指 Offer 11. 旋转数组的最小数字](https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/) | [剑指 Offer 12. 矩阵中的路径](https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof/) | [剑指 Offer 13. 机器人的运动范围](https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/) | [剑指 Offer 14- I. 剪绳子](https://leetcode-cn.com/problems/jian-sheng-zi-lcof/) | | | 思路 | 1,先排序,再返回数组第一个元素 `Arrays.sort(numbers); return numbers[0];` 2,二分法 ` int left = 0, right = numbers.length - 1; while (left < right) { int mid = (left + right) / 2; if (numbers[mid] > numbers[right]) left = mid + 1; else if (numbers[mid] < numbers[right]) right = mid; else right--; } return numbers[left]; ` | **深度优先遍历 DFS** | **深度优先遍历 DFS** | if (n == 1 \|\| n == 2) return 1; if (n == 3) return 2; int sum = 1; while (n > 4) { sum *= 3; n -= 3; } return sum * n; | 12,13,14 | | 第三天2022-04-01 | [剑指 Offer 14- II. 剪绳子 II](https://leetcode-cn.com/problems/jian-sheng-zi-ii-lcof/) | [剑指 Offer 15. 二进制中1的个数](https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/) | [剑指 Offer 16. 数值的整数次方](https://leetcode-cn.com/problems/shu-zhi-de-zheng-shu-ci-fang-lcof/) | [剑指 Offer 17. 打印从1到最大的n位数](https://leetcode-cn.com/problems/da-yin-cong-1dao-zui-da-de-nwei-shu-lcof/) | | | 思路 | 把绳子尽可能切为多个长度为 3 的片段,留下的最后一段绳子的长度可能为 0,1,2 三种情况。`if (n == 1 || n == 2) return 1; if (n == 3) return 2; long sum = 1; while (n > 4) { sum *= 3; sum %= 1000000007; n -= 3; } return (int) (sum * n % 1000000007);` | 逻辑与运算:**1,** int res = 0; while(n != 0) { res += n & 1; n >>>= 1; } return res; **2,**int res = 0; while(n != 0) { res += n & 1; n >>>= 1; // Java 中无符号右移为 ">>>>>>" } return res; | | | 16 | | 第四天2022-04-06 | [剑指 Offer 18. 删除链表的节点](https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/) | [剑指 Offer 19. 正则表达式匹配](https://leetcode-cn.com/problems/zheng-ze-biao-da-shi-pi-pei-lcof/) | [剑指 Offer 20. 表示数值的字符串](https://leetcode-cn.com/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof/) | [剑指 Offer 21. 调整数组顺序使奇数位于偶数前面](https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/) | | | 思路 | 递归:if (head == null) return null; if (head.val == val) return head.next; head.next = deleteNode(head.next, val); return head; | 困难 | //判定为数字,则标记numFlag if (s.charAt(i) >= '0' && s.charAt(i) <= '9') { numFlag = true; //判定为. 需要没出现过.并且没出现过e } else if (s.charAt(i) == '.' && !dotFlag && !eFlag) { dotFlag = true; //判定为e,需要没出现过e,并且出过数字了 } else if ((s.charAt(i) == 'e' \|\| s.charAt(i) == 'E') && !eFlag && numFlag) { eFlag = true; numFlag = false;//为了避免123e这种请求,出现e之后就标志为false //判定为+-符号,只能出现在第一位或者紧接e后面 } else if ((s.charAt(i) == '+' \|\| s.charAt(i) == '-') && (i == 0 \|\| s.charAt(i - 1) == 'e' \|\| s.charAt(i - 1) == 'E')) { //其他情况,都是非法的 | 双指针 left , right 分列数组左右两端,循环执行: 指针 left 从左向右寻找偶数; 指针 right 从右向左寻找奇数; 将 偶数 nums[left]和 奇数 nums[right]交换。 可始终保证: 指针 left 左边都是奇数,指针 right 右边都是偶数 。 | | | 第四天2022-04-07 | [剑指 Offer 22. 链表中倒数第k个节点](https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/) | [剑指 Offer 24. 反转链表](https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/) | [剑指 Offer 25. 合并两个排序的链表](https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/) | [剑指 Offer 26. 树的子结构](https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof/) | | | 思路 | 双指针 | 递归if (head == null \|\| head.next == null) return head; ListNode node = reverseList(head.next); head.next.next = head; head.next = null; return node; | 1,如果链表l1为null,直接返回l2 2,如果链表l2为null,直接返回l1 3,如果链表l1等于链表l2,返回链表l1 4,如果链表l1的值小于l2,返回l1,否则返回l2 | | | | 第五天2022-04-08 | [剑指 Offer 27. 二叉树的镜像](https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/) | [剑指 Offer 28. 对称的二叉树](https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/) | [剑指 Offer 29. 顺时针打印矩阵](https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/) | [剑指 Offer 30. 包含min函数的栈](https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/) | | | 思路 | 递归public TreeNode mirrorTree3(TreeNode root) { if (root == null) return null; TreeNode rootLeft = mirrorTree(root.left); TreeNode rootRight = mirrorTree(root.right); root.right = rootLeft; root.left = rootRight; return root; } | 递归。当 L 和 R 同时越过叶节点: 此树从顶至底的节点都对称,因此返回 true ; 当 L 或 R 中只有一个越过叶节点: 此树不对称,因此返回 false ; 当节点 L 值 != 节点 R 值: 此树不对称,因此返回 false ; | | 初始化两个栈。数据栈和辅助栈。辅助栈记录每次有元素进栈后或者出栈后,元素的最小值 | | | 第六天2022-04-10 | [剑指 Offer 31. 栈的压入、弹出序列](https://leetcode-cn.com/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof/) | [剑指 Offer 32 - I. 从上到下打印二叉树](https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/) | [剑指 Offer 32 - II. 从上到下打印二叉树 II](https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/) | [剑指 Offer 32 - III. 从上到下打印二叉树 III](https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/) | | | 思路 | 判断合不合法,用个栈试一试: 把压栈的元素按顺序压入,当栈顶元素和出栈的第一个元素相同,则将该元素弹出 | [队列](https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/solution/mian-shi-ti-32-i-cong-shang-dao-xia-da-yin-er-ch-4/) | | | | | | [剑指 Offer 34. 二叉树中和为某一值的路径](https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/) | [剑指 Offer 35. 复杂链表的复制](https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/) | [剑指 Offer 36. 二叉搜索树与双向链表](https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/) | [剑指 Offer 37. 序列化二叉树](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/) | | | | 先序遍历: 按照 “根、左、右” 的顺序,遍历树的所有节点。
路径记录: 在先序遍历中,记录从根节点到当前节点的路径。当路径为 ① 根节点到叶节点形成的路径 且 ② 各节点值的和等于目标值 sum 时,将此路径加入结果列表。
| | dfs(cur): 递归法中序遍历;终止条件: 当节点 cur 为空,代表越过叶节点,直接返回;
递归左子树,即 dfs(cur.left) ;
构建链表:
当 pre 为空时: 代表正在访问链表头节点,记为 head ;
当 pre 不为空时: 修改双向节点引用,即 pre.right = cur , cur.left = pre ;
保存 cur : 更新 pre = cur ,即节点 cur 是后继节点的 pre ;
递归右子树,即 dfs(cur.right) ; | | | | | [剑指 Offer 38. 字符串的排列](https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/) | [剑指 Offer 39. 数组中出现次数超过一半的数字](https://leetcode-cn.com/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/) | [剑指 Offer 40. 最小的k个数](https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/) | [剑指 Offer 41. 数据流中的中位数](https://leetcode-cn.com/problems/shu-ju-liu-zhong-de-zhong-wei-shu-lcof/) | | | 思路 | | 需要的数字出现次数多于一半 那么排序后必定在中间 class Solution { public int majorityElement(int[] nums) { Arrays.sort(nums); return nums[nums.length/2]; } } | 通俗易懂Arrays.sort(arr); return Arrays.copyOf(arr,k); | 优先队列 / 堆 | | | | [剑指 Offer 42. 连续子数组的最大和](https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof/) | [剑指 Offer 43. 1~n 整数中 1 出现的次数](https://leetcode-cn.com/problems/1nzheng-shu-zhong-1chu-xian-de-ci-shu-lcof/) | [剑指 Offer 44. 数字序列中某一位的数字](https://leetcode-cn.com/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/) | | | | | dp | | | | | | | | | | | | ``` ``` **必须会手写:快排,归并,堆排序**。快排的优化方法 掌握布隆过滤器,bit_map(思想和计数排序一样),在海量数据问题中会用到,海量数据问题是大厂面试特别喜欢考察的问题。 海量数据问题:数据量远远大于内存应该如何去进行解决,比如我抖音二面遇到的在内存受限(比如16M)情况下如何求十亿个整数的中位数还有很多海量数据问题例如在2.5亿个整数中找出不重复的整数(内存不足以容纳这2.5亿个整数)问题基本上都是分文件,哈希,排序(归并,快排,堆排),优先级队列,布隆过滤器,bit_map,前缀树中的几种组合。 如何对一个文件进行压缩(哈夫曼编码) ## 关于作者
微信 微信公众号
## Feature