Giter Site home page Giter Site logo

leetcode's Introduction

Problem Tag Level
1. Two Sum 用哈希表记住遍历过的元素、O(1)查找 Easy
2. Add Two Numbers 链表、遍历完两串中较长的那个、取默认值 Medium
5. Longest Palindromic Substring dp、中心点扩展法 Medium
6. ZigZag Conversion 格式化 Medium
8. String to Integer (atoi) 字符串处理、判断int溢出 Medium
11. Container With Most Water 首尾双指针、贪心 Medium
15. 3Sum 排序、首尾双指针、去掉重复的组合 Medium
17. Letter Combinations of a Phone Number 递归、回溯 Medium
18. 4Sum 排序、首尾双指针、去掉重复的组合🐛👍 Medium
19. Remove Nth Node From End of List 链表、快慢指针 Medium
20. Valid Parentheses 用栈来执行不断删除固定搭配然后拼接 Easy
21. Merge Two Sorted Lists 链表、递归处理 Easy
22. Generate Parentheses 回溯、dp Medium
23. Merge k Sorted Lists 合并链表、分治策略、最小堆 Hard
24. Swap Nodes in Pairs 链表反转、递归处理、用二级指针代替dummy结点 Medium
25. Reverse Nodes in k-Group 链表反转👍 Hard
28. Implement strStr() kmp算法👍 Easy
29. Divide Two Integers 数学、溢出、移位运算 Medium
33. Search in Rotated Sorted Array 广义二分查找 Medium
34. Find First and Last Position of Element in Sorted Array 二分查找 Medium
36. Valid Sudoku 多个容器同时标记/检查、9*9矩阵通过行号i和列号j计算3*3格子下标kk=i/3*3+j/3 Medium
39. Combination Sum 回溯 Medium
40. Combination Sum II 回溯 Medium
43. Multiply Strings 字符串乘法 Medium
46. Permutations 回溯、排列问题👍 Medium
47. Permutations II 回溯、排列问题、排序去重👍 Medium
48. Rotate Image 👍 Medium
49. Group Anagrams 计数 Medium
50. Pow(x, n) 数学 Medium
53. Maximum Subarray dp Easy
54. Spiral Matrix 格式化 Medium
55. Jump Game dp、贪心👍 Medium
56. Merge Intervals 没思路考虑一下排序 Medium
59. Spiral Matrix II 格式化 Medium
61. Rotate List 链表循环右移、把链表首位连起来 Medium
62. Unique Paths dp Medium
63. Unique Paths II dp Medium
70. Climbing Stairs dp Easy
73. Set Matrix Zeroes 👍 Medium
74. Search a 2D Matrix 物理二维存储=>逻辑一维二分查找 Medium
75. Sort Colors 首尾双指针 Medium
76. Minimum Window Substring 滑动窗口👍 Hard
78. Subsets 回溯 Medium
79. Word Search dfs👍 Medium
82. Remove Duplicates from Sorted List II 链表 Medium
86. Partition List 链表 Medium
91. Decode Ways dp👍 Medium
92. Reverse Linked List II 反转链表的递归代码 Medium
95. Unique Binary Search Trees II 树、dp、分治 Medium
96. Unique Binary Search Trees 树、dp Medium
98. Validate Binary Search Tree bst Medium
102. Binary Tree Level Order Traversal 记住层次的dfs和bfs Medium
103. Binary Tree Zigzag Level Order Traversal 另一种形式的记住层次的dfs、cpp移动语义避免拷贝堆内存、分情况计算数组下标 Medium
105. Construct Binary Tree from Preorder and Inorder Traversal 树👍 Medium
106. Construct Binary Tree from Inorder and Postorder Traversal Medium
109. Convert Sorted List to Binary Search Tree 链表、bst Medium
110. Balanced Binary Tree 平衡树 Easy
111. Minimum Depth of Binary Tree Easy
112. Path Sum Medium
113. Path Sum II 树、回溯👍 Medium
114. Flatten Binary Tree to Linked List 树👍 Medium
116. Populating Next Right Pointers in Each Node 树、层序遍历/bfs、特定于题目的指针层序遍历 Medium
117. Populating Next Right Pointers in Each Node II 树、链表👍 Medium
118. Pascal's Triangle Easy
119. Pascal's Triangle II 一维滚动数组代替二维数组👍 Easy
120. Triangle 滚动数组、记忆化dfs、dp👍 Medium
121. Best Time to Buy and Sell Stock One pass Easy
122. Best Time to Buy and Sell Stock II Easy
124. Binary Tree Maximum Path Sum 树👍 Hard
125. Valid Palindrome Easy
127. Word Ladder 将问题建模成图,然后dfs/bfs搜索、即时计算邻接结点👍 Medium
129. Sum Root to Leaf Numbers 树、dfs Medium
130. Surrounded Regions dfs、并查集 Medium
131. Palindrome Partitioning 回溯剪枝、遍历s[start, ]所有可能的子串👍 Medium
133. Clone Graph 图、dfs、bfs Medium
134. Gas Station 贪心 Medium
136. Single Number xor、哈希表 Easy
138. Copy List with Random Pointer 链表 Medium
139. Word Break dp、字典树O(N)加快字符串查找 Medium
140. Word Break II dp、回溯 Hard
141. Linked List Cycle 哈希表、快慢指针 Easy
142. Linked List Cycle II floyd龟兔算法检测环、哈希表、快慢指针 Medium
144. Binary Tree Preorder Traversal 树、Morris算法👍 Medium
145. Binary Tree Postorder Traversal 树、Morris算法👍 Hard
146. LRU Cache 双向链表、哈希表 Medium
147. Insertion Sort List 链表、排序 Medium
148. Sort List 链表、排序 Medium
150. Evaluate Reverse Polish Notation 栈、计算逆波兰表达式、std::stoi、std::to_string Medium
151. Reverse Words in a String 字符串处理、双指针压缩多余空格🐛👍 Medium
152. Maximum Product Subarray dp Medium
153. Find Minimum in Rotated Sorted Array 非常规二分查找 Medium
155. Min Stack 单链表实现栈👍 Easy
160. Intersection of Two Linked Lists 链表 Easy
162. Find Peak Element 非常规二分查找👍 Medium
165. Compare Version Numbers istringstream、遍历完两串中较长的那个、取默认值 Medium
166. Fraction to Recurring Decimal 结果为小数的除法过程、哈希表记住出现过的元素👍 Medium
167. Two Sum II - Input array is sorted 首尾双指针 Easy
168. Excel Sheet Column Title 偏移量为1的进制👍 Easy
169. Majority Element 排序、哈希表计数、Boyer-Moore Voting Algorithm👍 Easy
171. Excel Sheet Column Number 偏移量为1的进制 Easy
172. Factorial Trailing Zeroes 👍 Easy
173. Binary Search Tree Iterator bst、Morris算法👍 Medium
175. Combine Two Tables sql、join👍 Easy
176. Second Highest Salary sql、投影是最后执行的👍 Easy
178. Rank Scores sql、join、问题转换(排名=>有多少个成绩比该记录高)👍 Medium
179. Largest Number 排序👍 Medium
189. Rotate Array 👍 Medium
190. Reverse Bits 位操作 Easy
191. Number of 1 Bits 位操作 Easy
198. House Robber dp Easy
199. Binary Tree Right Side View dfs/bfs👍 Medium
200. Number of Islands 判断连通分量个数、dfs/bfs、并查集 Medium
202. Happy Number 哈希表、floyd龟兔算法检测环/快慢指针 Easy
204. Count Primes 筛法选素数 Easy
206. Reverse Linked List 反转链表的三种方法 Easy
207. Course Schedule 拓扑排序 Medium
208. Implement Trie (Prefix Tree) 字典树 Medium
209. Minimum Size Subarray Sum 滑动窗口、原数组无序,但前缀和数组有序,转换问题配合二分查找👍 Medium
210. Course Schedule II 拓扑排序 Medium
211. Add and Search Word - Data structure design 字典树👍 Medium
212. Word Search II 字典树、dfs、回溯👍 Hard
213. House Robber II dp👍 Medium
215. Kth Largest Element in an Array 堆、排序、快速排序**👍 Medium
216. Combination Sum III 回溯、哈希表O(1)记住用过的元素 Medium
217. Contains Duplicate 没思路考虑排序、查重可用哈希表 Easy
219. Contains Duplicate II 滑动窗口+哈希表 Easy
220. Contains Duplicate III 滑动窗口+哈希表、elem/bucketSize将元素映射到桶中、滑动窗口的元素从elem改为桶、滑动窗口+红黑树/平衡bst、多次std::set::lower_bound不同参数进行O(logN)范围查找👍 Medium
222. Count Complete Tree Nodes 完全二叉树的子树中至少一个为完全二叉树,另一个可能是也可能不是👍 Medium
224. Basic Calculator Medium
226. Invert Binary Tree Easy
227. Basic Calculator II 运算优先级、将+-看作一元运算、栈、istringstream自动忽略空白符和方便地解析数字字符串(包括浮点数字符串、科学计数法字符串) Medium
229. Majority Element II Boyer-Moore Voting Algorithm👍 Medium
230. Kth Smallest Element in a BST bst Medium
234. Palindrome Linked List 快慢指针边遍历边反转链表、回文数 Easy
235. Lowest Common Ancestor of a Binary Search Tree lca、bst👍 Medium
236. Lowest Common Ancestor of a Binary Tree lca👍 Medium
237. Delete Node in a Linked List 链表、在未知前驱结点的情况下,删除当前结点…… Easy
238. Product of Array Except Self 前缀积数组 Medium
239. Sliding Window Maximum 滑动窗口+双端队列👍 Hard
240. Search a 2D Matrix II 巧妙、立体的二分查找 Medium
241. Different Ways to Add Parentheses 分治策略👍 Medium
242. Valid Anagram 哈希表/数组 Easy
257. Binary Tree Paths Easy
268. Missing Number 排序、哈希表、位操作、等差数列求和 Easy
279. Perfect Squares dp、很有启发的bfs解法 Medium
283. Move Zeroes 双指针、移动元素后剩余覆盖零 Easy
287. Find the Duplicate Number 基于range的二分查找、问题的巧妙转换、floyd龟兔算法检测环👍 Medium
289. Game of Life 对状态转移进行编码、问题扩展 Medium
290. Word Pattern 字符串处理、istringstream方便地读取单词和转换数字字符串为整型 Easy
297. Serialize and Deserialize Binary Tree 树、序列化反序列化👍 Hard
300. Longest Increasing Subsequence dp Medium
303. Range Sum Query - Immutable 前缀和 Easy
322. Coin Change dp的两个方向(从问题出发/记忆化dfs、从边界出发)、非常规完全背包问题/最优化问题 Medium
326. Power of Three 模运算、math Easy
328. Odd Even Linked List 链表分类 Medium
329. Longest Increasing Path in a Matrix 两个方向的dp、最大堆、拓扑排序(值得学习的分析角度) Hard
337. House Robber III dp、树👍 Medium
338. Counting Bits dp Medium
334. Increasing Triplet Subsequence 存在性问题、考虑最容易满足的情况、双指针、前缀最小后缀最大枚举a<b<c中的b🐂👍 Medium
341. Flatten Nested List Iterator 栈(后进的元素反而需要先访问/处理) Medium
344. Reverse String 双指针 Easy
347. Top K Frequent Elements 最小堆(大小为k的堆查找插入删除为O(logK))、哈希表统计频率/计数 Medium
350. Intersection of Two Arrays II 哈希表、双指针、followup Easy
371. Sum of Two Integers 位操作 Easy
377. Combination Sum IV 典型背包问题是组合问题,这道题类似背包问题,但是排列问题 Medium
378. Kth Smallest Element in a Sorted Matrix 基于range的二分查找、最小堆 Medium
380. Insert Delete GetRandom O(1) 哈希表O(1)给定元素得到元素在数组中的位置、swap到数组末尾再删除避免移动数组元素达到O(1)时间复杂度(不需要真的交换) Medium
381. Insert Delete GetRandom O(1) - Duplicates allowed 两个互关联的容器 Hard
384. Shuffle an Array std::rand、生成一定范围的随机数、Fisher–Yates shuffle、证明👍 Medium
387. First Unique Character in a String 哈希表计数 Easy
392. Is Subsequencey 双指针 Easy
393. UTF-8 Validation 位运算 Medium
395. Longest Substring with At Least K Repeating Characters 滑动窗口、分治策略👍 Medium
404. Sum of Left Leaves Easy
412. Fizz Buzz 红黑树(std::set, std::map)/自平衡二叉搜索树👍 Easy
424. Longest Repeating Character Replacement 滑动窗口+哈希表计数👍 Medium
429. N-ary Tree Level Order Traversal bfs Medium
430. Flatten a Multilevel Doubly Linked List 链表 Medium
435. Non-overlapping Intervals 区间问题 Medium
437. Path Sum III 树、前缀和哈希表、连续子序列和👍 Easy
445. Add Two Numbers II 链表、栈间接反转链表、头插法创建逆序链表 Medium
449. Serialize and Deserialize BST bst、序列化反序列化、i/ostringstream👍 Medium
450. Delete Node in a BST bst👍 Medium
451. Sort Characters By Frequency 哈希表、排序、堆 Medium
454. 4Sum II 组合问题👍 Medium
496. Next Greater Element I Easy
501. Find Mode in Binary Search Tree bst👍 Easy
503. Next Greater Element II Medium
508. Most Frequent Subtree Sum Medium
509. Fibonacci Number dp Medium
513. Find Bottom Left Tree Value 树、bfs Medium
515. Find Largest Value in Each Tree Row 树、bfs Medium
518. Coin Change 2 dp、完全背包问题 Medium
538. Convert BST to Greater Tree 中序遍历(后中左)👍 Easy
543. Diameter of Binary Tree 树👍 Easy
559. Maximum Depth of N-ary Tree Easy
560. Subarray Sum Equals K 前缀和哈希表、连续子序列和👍 Medium
563. Binary Tree Tilt 树、自顶向下/自底向上👍 Easy
566. Reshape the Matrix 数组、一二维转换 Easy
572. Subtree of Another Tree 树、对树进行唯一编码👍 Easy
589. N-ary Tree Preorder Traversal 树👍 Easy
590. N-ary Tree Postorder Traversal 树👍 Easy
617. Merge Two Binary Trees Easy
623. Add One Row to Tree 树、dfs/bfs👍 Medium
637. Average of Levels in Binary Tree 树、bfs Easy
652. Find Duplicate Subtrees 树、序列化👍 Medium
653. Two Sum IV - Input is a BST 树、首尾双指针、哈希表 Easy
654. Maximum Binary Tree 树、栈中维护下降序列 Medium
662. Maximum Width of Binary Tree 树👍 Medium
669. Trim a Binary Search Tree 树、bst👍 Medium
670. Maximum Swap 数据量小可考虑暴力破解、贪心+数组 Medium
671. Second Minimum Node In a Binary Tree 树👍 Easy
684. Redundant Connection 树、图、并查集👍 Medium
685. Redundant Connection II 树、图、并查集👍 Hard
687. Longest Univalue Path 树👍 Easy
692. Top K Frequent Words 哈希表统计/计数、堆 Medium
698. Partition to K Equal Sum Subsets 回溯、dfs、dp👍 Medium
700. Search in a Binary Search Tree bst Easy
701. Insert into a Binary Search Tree bst Medium
717. 1-bit and 2-bit Characters Easy
725. Split Linked List in Parts 链表分割 Medium
739. Daily Temperatures Medium
746. Min Cost Climbing Stairs dp Easy
766. Toeplitz Matrix 矩阵对角线 Easy
783. Minimum Distance Between BST Nodes bst👍 Easy
779. K-th Symbol in Grammar 递归定义的问题👍 Medium
817. Linked List Components 链表、多角度考虑问题 Medium
820. Short Encoding of Words 字典树、单词后缀问题 Medium
859. Buddy Strings 字符串处理、题意理解👍 Easy
872. Leaf-Similar Trees 树、迭代bfs Easy
876. Middle of the Linked List 链表、快慢指针 Easy
877. Stone Game dp👍 Medium
885. Spiral Matrix III 格式化 Medium
889. Construct Binary Tree from Preorder and Postorder Traversal 建树👍 Medium
894. All Possible Full Binary Trees 带缓存的分治策略 Medium
897. Increasing Order Search Tree 返回两个指针,一个是树的根指针,一个是指向在某种遍历(先序/中序/后序)下最后遍历的那个结点的指针👍 Medium
919. Complete Binary Tree Inserter cbt👍 Medium
938. Range Sum of BST bst、range👍 Easy
944. Delete Columns to Make Sorted 遍历行列 Easy
1003. Check If Word Is Valid After Substitutions 用字符栈来执行不断删除固定搭配然后拼接 Medium
1019. Next Greater Node In Linked List 链表、栈 Medium
1023. Camelcase Matching 字符串匹配 Medium
1025. Divisor Game dp Easy
1042. Flower Planting With No Adjacent 图、用数组检查已被用过的元素 Easy
1052. Grumpy Bookstore Owner 滑动窗口 Medium
1103. Distribute Candies to People 模运算绕圈 Easy
1004. Max Consecutive Ones III 滑动窗口+哈希表计数👍 Medium
1114. Print in Order 多线程、同步 Easy
1115. Print FooBar Alternately 多线程、信号量代替计数器+条件变量👍 Medium
1116. Print Zero Even Odd 多线程、信号量同步👍 Medium
1117. Building H2O 多线程、信号量同步👍 Medium
1130. Minimum Cost Tree From Leaf Values dp Medium
1137. N-th Tribonacci Number Easy
1171. Remove Zero Sum Consecutive Nodes from Linked List 链表、前缀和哈希表删除和为0的子序列👍 Medium
1203. Sort Items by Groups Respecting Dependencies 双层拓扑排序👍 Hard
1261. Find Elements in a Contaminated Binary Tree 空间换时间、哈希表O(1)缓存结果而不是每次查询都dfs👍 Medium
1277. Count Square Submatrices with All Ones dp Medium
1302. Deepest Leaves Sum 树、dfs/bfs Medium
1305. All Elements in Two Binary Search Trees 树、多次bfs/dfs、迭代和递归结合的中序遍历👍 Medium
1315. Sum of Nodes with Even-Valued Grandparent Medium
1325. Delete Leaves With a Given Value 树、后续遍历 Medium
1367. Linked List in Binary Tree 树、kmp算法👍 Medium
1394. Find Lucky Integer in an Array 哈希表计数/reduce Easy
1395. Count Number of Teams 组合问题👍 Medium
1396. Design Underground System Medium
  • 设计递归算法时,从宏观上设计/观察递归函数的输入、输出。然后具体地,再设计递归边界和子问题。如递归函数swapPairs的输入是一个普通链表,输出是一个已经两两交换过的链表。那么我们就可以从宏观上去调用/使用该函数,而不必纠结内部实现细节以及递归的繁琐的展开和求值过程,反正我们已经知道/能够保证该递归函数的接收某个输入必定产出对应的输出即可。(24)

  • 对于快慢指针遍历链表,对于偶数个结点的链表,如果从dummy结点开始走,那么结束时,slow指针指向中间两个结点中的前者,否则指向中间两个结点中的后者。对于奇数个结点的链表,则没有区别,都是结束时slow指针指向中间的那个结点。

  • 反转链表的三种方法

  • 链表题考查的边界和易错情况:

    • Dereferencing Null Pointer, usually targeting tail pointer
    • When given Head is None
    • When there are duplications in the list
  • 注意这里计数的角度,后面的解法是遍历每个cell,然后遍历该cell的周边来计数该cell周边的1的个数;而这里是遍历每个为1的cell,然后增加其周边的cell的“周边为1的cell的个数的计数器”。(289)

  • https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173/Share-my-thoughts-and-Clean-Java-Code

    The key point for any binary search is to figure out the "Search Space". For me, I think there are two kind of "Search Space" -- index and range(the range from the smallest number to the biggest number). Most usually, when the array is sorted in one direction, we can use index as "search space", when the array is unsorted and we are going to find a specific number, we can use "range".

    Let me give you two examples of these two "search space"

    1. index -- A bunch of examples -- https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ ( the array is sorted)
    2. range -- https://leetcode.com/problems/find-the-duplicate-number/ (Unsorted Array)(也适用于非一维序列,或者不方便映射到一维的多维序列)
  • 典型多路径搜索问题。多路径搜索,如果只是用嵌套循环的话,难以实现,但是如果把问题建模成树或图,然后用dfs/bfs进行搜索,这种思路往往更容易实现。

    • 127. Word Ladder,将该问题建模成无向图,然后dfs/bfs进行搜索。输入的wordList中每个string是一个结点,如果两个string长度相等且有且只有一个字符不同,则这两个结点是邻接的/相连的。
  • 给出一个中点x,查看set中是否有一个元素在[x-t, x+t]的范围内,只需一次O(logN)查找即可。

    • 220. Contains Duplicate III

      // it = s.lower_bound(nums[i]);
      // if (it!=s.end() && *it-nums[i]<=t) return true;
      it = s.lower_bound(static_cast<long>(nums[i])-t);
      if (it!=s.end() && abs(static_cast<long>(nums[i])-*it)<=t) return true;
  • // insert
    p->isEnd = true;
    
    // search
    return p->isEnd; // XXX 注意这里不能直接返回true,如果p->isEnd为false,说明这个word只是集合中某个字符串的前缀而已,该集合中并不存在该word。
  • Using Cyclic Replacements:

  • dfs/bfs搜索问题,注意不要走回头路或绕圈,可用visited数组防止,然后在调用后检查可以使得调用方代码干净一些,当然调用前检查会有少量效率提升。

  • 组合问题,排序去重。排序时间复杂度小于整体时间复杂度,可大胆考虑排序。

  • 滑动窗口,哈希表计数

    In any sliding window based problem we have two pointers. One right pointer whose job is to expand the current window and then we have the left pointer whose job is to contract a given window. At any point in time only one of these pointers move and the other one remains fixed.

    Here is a 10-line template that can solve most 'substring' problems.

  • 空树是什么?就是没有任何结点的树,也就是指针值为nullptr。若指针值不为nullptr,则至少指向一个结点,就不是空树。

  • TODO:再理解一下(完全)背包问题相关的题。

  • 分治策略:

  • TODO:看树栈迭代遍历。

  • 树的两种搜索方式:

  • 前缀和哈希表,连续子序列和:

  • 递归定义的问题:

  • 使用istringstream,可以自动忽略空白符(包括前导空白符),很方便将数字字符串输出到整型或浮点型(科学计数法也能解析):

  • 一棵树匹配另一棵树或链表或数组等,从树A的某一点开始匹配,如果匹配失败,需要回来到起点,然后从树A的下一个结点继续尝试匹配。所以一般需要两个函数,一个主函数挑选起点,一个从该起点开始匹配。

  • 给定一个结点值,在bst中搜索的迭代实现是很容易的,因为根据bst的性质,每次可以在两个方向/子树中明确一个方向,而普通二叉树则无法给出一个值,在一个根处确定两个方向中的一个,可能选错了子树,就需要回来搜另一棵子树。

  • 返回两个指针,一个是树的根指针,一个是指向在某种遍历(先序/中序/后序)下最后遍历的那个结点的指针。

  • 栈中维护下降序列。

leetcode

leetcode's People

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.