Leetcode刷题

https://leetcode.com/problemset/all/

1. 知识储备

1.1. 语言选择

  • ⭐️C++:向下兼容C语言,拥有多个封装好的函数
  • Java

1.2. 算法思想

双指针

排序

贪心思想

二分查找

分治

搜索(DFS,BFS)

动态规划

数学知识

1.3. 数据结构

官网总结

2. 如何刷题

按类型tag刷题,每个类型10-20题(动态规划:多多益善)

周一:树/链表 (树最容易培养框架思维-递归/DFS/BFS)

周二:搜索

周三:动态规划 …

第一遍:5分钟想不出来就看答案

第二遍:尝试不看答案完整实现,不超过60分钟

第三遍:快速实现

看别人的代码(论坛),至少3-5种不同的实现,分析优缺点,速度快慢

学习新的语言,算法,数据结构,API,模版

完整的手写实现,不要copy代码,增强肌肉记忆

运行时间70%左右的代码,阅读提高能力

扩宽思路-使用什么算法,推算时间复杂度,统一代码风格,取有意义的变量名

  1. 《剑指offer》覆盖基本题型

  2. Top 100 Liked 高频题 ,Top 100 interview 经典题

  3. 重复刷2-3遍,看到题就有思路

  4. 按照公司tag刷,facebook,google,amazon - 一亩三分地

  5. 公司面经

3. 网站推荐

Up主:土妹编程 花花酱的表世界

CSNotes:https://github.com/CyC2018/CS-Notes

牛客网:https://www.nowcoder.com/ta/job-code-high-week

codetop:https://codetop.cc/home

leetcode:

官网 https://leetcode.com/problemset/all/

中文 https://leetcode-cn.com/problemset/all/

4. 挑战赛

leetcode周赛 https://leetcode.com/contest/

牛客月赛

codetop按公司及岗位

5. 解析思路

https://zxi.mytechroad.com/blog/leetcode-problem-categories/

5.1. Tree(树)

Id Name Difficulty Similar Problems Comments
94 Binary Tree Inorder Traversal 144 145 429 589 590 traversal
100 Same Tree ★★ 101 110 111 572 965
102 Binary Tree Level Order Traversal ★★ 107 429 872 collecting nodes
814 Binary Tree Pruning ★★★ 669 1325
112 Path Sum ★★★ 113 437
129 Sum Root to Leaf Numbers ★★★ 257
236 Lowest Common Ancestor of a Binary Tree ★★★ 235
297 Serialize and Deserialize Binary Tree ★★★ 449
508 Most Frequent Subtree Sum ★★★
124 Binary Tree Maximum Path Sum ★★★ 543 687 Use both children, return one
968 Binary Tree Cameras ★★★★ 337 979

5.2. Divide and conquer(分治)

Id Name Difficulty Similar Problems Comments
169 Majority Element ★★ 你知道茴香豆的茴有几种写法吗?
153 Find Minimum in Rotated Sorted Array ★★ 154
912 Sort and Array ★★★ merge sort
315 Count of Smaller Numbers After Self ★★★★ merge sort / BIT

5.3. List(链表)

Id Name Difficulty Similar Problems Comments
2 Add Two Numbers ★★ 445 traversal
24 Swap Nodes in Pairs ★★ reverse
206 Reverse Linked List ★★ reverse
141 Linked List Cycle ★★ 142 priority_queue / mergesort
23 Merge k Sorted Lists ★★★ 21 priority_queue / mergesort
147 Insertion Sort List ★★★ insertion sort
148 Sort List ★★★★ merge sort O(1) space
707 Design Linked List ★★★★

5.4. BST(二叉搜索树)

Id Name Difficulty Similar Problems Comments
98 Validate Binary Search Tree ★★ 530 inorder
700 Search in a Binary Search Tree ★★ 701 binary search
230 Kth Smallest Element in a BST ★★★ inorder
99 Recover Binary Search Tree ★★★ inorder
108 Convert Sorted Array to Binary Search Tree ★★★ build BST
501 Find Mode in Binary Search Tree ★★★ inorder
450 Delete Node in a BST ★★★★ binary search

5.5. Graph(图论)

Id Name Difficulty Similar Problems Comments
133 Clone Graph ★★ 138
200 Number of Islands ★★ 547 695 733 827 1162 grid + connected components
841 Keys and Rooms ★★ 1202
207 Course Schedule ★★★ 210 802
399 Evaluate Division ★★★ 839 952 990 721 737 union find
785 Is Graph Bipartite? ★★★ 886 1042 bipartition, graph coloring
997 Find the Town Judge ★★★ in/out degrees
433 Minimum Genetic Mutation ★★★ 815 863 1129 1263 unweighted shortest path / BFS
684 Redundant Connection ★★★★ 685 1319 cycle, union find
743 Network Delay Time ★★★★ 787 882 924 1334 weighted shortest path
847 Shortest Path Visiting All Nodes ★★★★ 864 1298 BFS
332 Reconstruct Itinerary ★★★★ Eulerian path
1192 Critical Connections in a Network ★★★★ Tarjan
943 Find the Shortest Superstring ★★★★★ 980 996 Hamiltonian path (DFS / DP)
959 Regions Cut By Slashes ★★★★★ union find / grid + CCs

5.6. Search (BFS/DFS)(搜索/回溯)

Id Name Difficulty Similar Problems Comments
17 Letter Combinations of a Phone Number ★★ 39 40 77 78 90 216 Combination
46 Permutations ★★ 47 784 943 996 Permutation
22 Generate Parentheses ★★★ 301 DFS
37 Sudoku Solver ★★★ 51 52 DFS
79 Word Search ★★★ 212 DFS
127 Word Ladder ★★★★ 126 752 818 BFS
542 01 Matrix ★★★ 675 934 BFS
698 Partition to K Equal Sum Subsets ★★★ 93 131 241 282 842 Partition

5.7. Binary Search(二分搜索)

Id Name Difficulty Similar Problems Comments
35 Search Insert Position ★★ 34 704 981 upper_bound
33 Search in Rotated Sorted Array ★★★ 81 153 154 162 852 rotated / peak
69 Sqrt(x) ★★★ upper_bound
74 Search a 2D Matrix ★★★ treat 2d as 1d
875 Koko Eating Bananas ★★★ 1011 guess ans and check
4 Median of Two Sorted Arrays ★★★★
378 Kth Smallest Element in a Sorted Matrix ★★★★ 668 kth + matrix
719 Find K-th Smallest Pair Distance ★★★★ 786 kth + two pointers

5.8. Two Pointers(双指针)

Id Name Difficulty Similar Problems Comments
11 Container With Most Water ★★ 42
125 Valid Palindrome ★★ 455
917 Reverse Only Letters ★★ 925 986 855
167 Two Sum II – Input array is sorted ★★★ 15 16
977 Squares of a Sorted Array ★★ merge sort
992 Subarrays with K Different Integers ★★★★

5.9. Advanced(高级)

Id Name Difficulty Similar Problems Comments
208 Implement Trie (Prefix Tree) ★★★ 648 676 677 720 745 211 Trie
307 Range Sum Query – Mutable ★★★ BIT/Segment Tree
901 Online Stock Span ★★★ 907 1019 monotonic stack
239 Sliding Window Maximum ★★★ monotonic queue

5.10. DP(动态规划)

Id Name Difficulty Similar Problems Comments
70 Climbing Stairs 746 1137 I: O(n), S = O(n), T = O(n)
303 Range Sum Query – Immutable 1218 I: O(n), S = O(n), T = O(n)
53 Maximum Subarray ★★ 121 I: O(n), S = O(n), T = O(n)
62 Unique Paths ★★ 63 64 120 174 931 1210 I: O(mn), S = O(mn), T = O(mn)
85 Maximal Rectangle ★★★ 221 304 1277 I: O(mn), S = O(mn), T = O(mn)
198 House Robber ★★★ 213 309 740 790 801 I: O(n), S = O(3n), T = O(3n)
279 Perfect Squares ★★★ I: n, S = O(n), T = O(n*sqrt(n))
139 Word Break ★★★ 140 818 I: O(n), S = O(n), T = O($n^2$)
300 Longest Increasing Subsequence ★★★ 673 1048 I: O(n), S = O(n), T = O($n^2$)
96 Unique Binary Search Trees ★★★ I: O(n), S = O(n), T = O($n^2$)
1105 Filling Bookcase Shelves ★★★ I: O(n) + t, S = O(n), T = O($n^2$)
131 Palindrome Partitioning ★★★ 89 I: O(n), S = O(2^n), T = O($2^n$)
72 Edit Distance ★★★ 10 44 97 115 583 1187 1143 1092 718 712 I: O(m+n), S = O(mn), T = O(mn)
1139 Largest 1-Bordered Square ★★★ I: O(mn), S = O(mn) T = O(mn*min(n,m))
688 Knight Probability in Chessboard ★★★ 576 935 I: O(mn) + k S = O(kmn), T = O(kmn)
322 Coin Change ★★★ 377 416 494 1043 1049 1220 1230 1262 1269 I: O(n) + k, S = O(n), T = O(kn)
813 Largest Sum of Averages ★★★★ 1278 1335 410 I: O(n) + k,S = O(nk), T = O(k$n^2$)
1223 Dice Roll Simulation ★★★★ I: O(n) + k + p,S = O(kp), T = O($n^2$kp)
312 Burst Balloons ★★★★ 664 1024 1039 1140 1130 I: O(n), S = O($n^2$), T = O($n^3$)
741 Cherry Pickup ★★★★ I: O($n^2$), S = O($n^3$), T = O($n^3$)
546 Remove Boxes ★★★★★ I: O(n), S = O($n^3$), T = O($n^4$)
943 Find the Shortest Superstring ★★★★★ 980 996 1125 I: O(n),S = O(nx$2^n$), T = ($n^2$*$2^n$)
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2018/09/22/Leetcode刷题/
  • 发布时间: 2018-09-22 20:01
  • 更新时间: 2023-04-18 20:15
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!