1 |
Two Sum |
C++ |
|
2 |
Add Two Numbers |
JAVA |
|
3 |
Longest Substring Without Repeating Characters |
JAVA |
维护前后两个指针 |
5 |
Longest Palindromic Substring |
JAVA |
dp做O(n^2),Manacher算法可以做到O(n) |
6 |
ZigZag Conversion |
JAVA |
|
7 |
Reverse Integer |
JAVA |
神TM样例还越界了,最后居然上了trycatch |
8 |
String to Integer (atoi) |
JAVA |
|
9 |
Palindrome Number |
JAVA |
|
10 |
Container With Most Water |
JAVA |
h[l]小于h[r]时左移l,舍弃了(l,r-1)(l,r-2)..这样的区间,这样的区间是比(l,r)的值小的 |
11 |
Integer to Roman |
JAVA |
|
12 |
Roman to Integer |
JAVA |
|
14 |
Longest Common Prefix |
JAVA |
|
15 |
3Sum |
JAVA |
快排nlgn,枚举一个数再维护前后两个指针遍历一共O(n^2),再处理一些重复答案的细节 |
16 |
3Sum Closest |
JAVA |
和上道题思路相同 |
17 |
Letter Combinations of a Phone Number |
JAVA |
dfs |
18 |
4Sum |
JAVA |
和15思路相同O(n^3),再预判断一些可能的最小值和最大值可以大幅减少时间 |
19 |
Remove Nth Node From End of List |
JAVA |
前后两个指针,后指针到底时删前指针的,头部单独处理 |
20 |
Valid Parentheses |
JAVA |
|
21 |
Merge Two Sorted Lists |
JAVA |
递归从尾部开始 |
22 |
Generate Parentheses |
JAVA |
顺手测了下sb还是比string快多了 |
24 |
Swap Nodes in Pairs |
JAVA |
|
26 |
Remove Duplicates from Sorted Array |
JAVA |
|
27 |
Remove Element |
JAVA |
|
28 |
Implement strStr() |
JAVA |
KMP |
29 |
Divide Two Integers |
JAVA |
O(logn)实现除法 |
31 |
Next Permutation |
JAVA |
根据全序列的定义模拟 |
34 |
Search for a Range |
JAVA |
左右二分查找 |
35 |
Search Insert Position |
JAVA |
|
36 |
Valid Sudoku |
JAVA |
用二维数组来标记TF |
38 |
Count and Say |
JAVA |
|
39 |
Combination Sum |
JAVA |
dfs |
40 |
Combination Sum II |
JAVA |
|
43 |
Multiply Strings |
JAVA |
大数相乘的逻辑,没处理好逻辑WA了好多发,Karatsuba可以做到O(n^1.58) |
46 |
Permutations |
JAVA |
dfs不好处理已经用过的数,用数字对换做比较方便 |
47 |
Permutations II |
JAVA |
没想通重复的原因,在46的基础上加了个答案判重,效率极低,哪天智商高点的时候再来想想 |
47 |
Permutations II |
JAVA |
不用对换了,上dfs了,重复数字必须从前往后搜索 |
48 |
Rotate Image |
JAVA |
|
49 |
Group Anagrams |
JAVA |
HashMap的使用、遍历;String、List的排序 |
50 |
Pow(x, n) |
[JAVA](./pow(x, n)/Pow.java) |
先写了理论上也是二分的超时了,原因改天再想,换了种思路二分递归 |
53 |
Maximum Subarray |
JAVA |
O(n)最大子数组 |
54 |
Spiral Matrix |
JAVA |
模拟 |
55 |
Jump Game |
JAVA |
|
58 |
Length of Last Word |
JAVA |
|
59 |
Spiral Matrix II |
JAVA |
|
60 |
Permutation Sequence |
JAVA |
神TM时间超过了99%的提交,写的有点难懂,大概就是取(n-i)!来判断第i位是什么 |
61 |
Rotate List |
JAVA |
|
62 |
Unique Paths |
JAVA |
dp |
63 |
Unique Paths II |
JAVA |
dp |
64 |
Minimum Path Sum |
JAVA |
dp三连 |
66 |
Plus One |
JAVA |
|
67 |
Add Binary |
JAVA |
|
69 |
Sqrt(x) |
JAVA |
|
70 |
Climbing Stairs |
JAVA |
dp |
71 |
Simplify Path |
JAVA |
|
73 |
Set Matrix Zeroes |
JAVA |
|
74 |
Search a 2D Matrix |
JAVA |
标准二分 |
75 |
Sort Colors |
JAVA |
|
77 |
Combinations |
JAVA |
|
78 |
Subsets |
JAVA |
|
79 |
Word Search |
JAVA |
|
80 |
Remove Duplicates from Sorted Array II |
JAVA |
|
33 |
Search in Rotated Sorted Array |
JAVA |
二分,逻辑有点乱,自己都不知道怎么过的 |
81 |
Search in Rotated Sorted Array II |
JAVA |
上一道题的加强版,逻辑理清了二分还是很简单的 |
82 |
Remove Duplicates from Sorted List II |
JAVA |
做了补头不补头两个版本,这种涉及头部变化的最好补头,不然逻辑很乱 |
83 |
Remove Duplicates from Sorted List |
JAVA |
上一道题的弱化版,最直观的就是头部不会变化了,这种就不用补头,而且特简单 |
86 |
Partition List |
JAVA |
现在想想只要不重新申请一个链表出来,申请个头部、几个指针什么的还是合理的,而且会让问题很简单,没有必要非要在原链表上进行直接操作 |
88 |
Merge Sorted Array |
JAVA |
从后往前避免覆盖 |
89 |
Gray Code |
JAVA |
|
90 |
Subsets II |
JAVA |
78上加一行 |
91 |
Decode Ways |
JAVA |
dp |
92 |
Reverse Linked List II |
JAVA |
三个指针翻转 |
93 |
Restore IP Addresses |
JAVA |
|
94 |
Binary Tree Inorder Traversal |
JAVA |
标准中序遍历 |
95 |
Unique Binary Search Trees II |
JAVA |
二叉搜索树的生成,递归有点绕,回头再做一遍(而且做的其实不严谨,这里面的下层节点都用的一个引用,这些生成的搜索树都没有分开) |
96 |
Unique Binary Search Trees |
JAVA |
95的逻辑,dp |
98 |
Validate Binary Search Tree |
JAVA |
BST的定义是左子树的所有节点都要比根节点小,右同,因此用min、max对子树进行界限规定,使用Integer可取null值表示不受限 |
98 |
Validate Binary Search Tree |
JAVA |
BST的中序遍历是递增数列,以此判断,效率不如上面的做法 |
100 |
Same Tree |
JAVA |
|
101 |
Symmetric Tree |
JAVA |
100改一行 |
102 |
Binary Tree Level Order Traversal |
JAVA |
bfs |
103 |
Binary Tree Zigzag Level Order Traversal |
JAVA |
bfs,往头部添加元素用LinkedList |
104 |
Maximum Depth of Binary Tree |
JAVA |
bfs |
104 |
Maximum Depth of Binary Tree |
JAVA |
dfs |
105 |
Construct Binary Tree from Preorder and Inorder Traversal |
JAVA |
前序中序构造树 |
105 |
Construct Binary Tree from Preorder and Inorder Traversal |
JAVA |
这道题测试用例的原因,导致把从前往后查找改成从后往前查找就超过了99%的提交,我还以为那些快的是什么新方法呢 |
106 |
Construct Binary Tree from Inorder and Postorder Traversal |
JAVA |
中序后序构造树 |
107 |
Binary Tree Level Order Traversal II |
JAVA |
|
108 |
Convert Sorted Array to Binary Search Tree |
JAVA |
|
109 |
Convert Sorted List to Binary Search Tree |
JAVA |
快慢指针+二分O(nlogn)超时了,转数组后108解O(n)即可 |
56 |
Merge Intervals |
JAVA |
|
110 |
Balanced Binary Tree |
JAVA |
|
111 |
Minimum Depth of Binary Tree |
JAVA |
|
112 |
Path Sum |
JAVA |
|
113 |
Path Sum II |
JAVA |
|
114 |
Flatten Binary Tree to Linked List |
JAVA |
|
116 |
Populating Next Right Pointers in Each Node |
JAVA |
bfs |
116 |
Populating Next Right Pointers in Each Node |
JAVA |
dfs递归 |
117 |
Populating Next Right Pointers in Each Node II |
JAVA |
116 bfs 一模一样 |