- strStr
- strStr II
- Subsets
- Subsets II
- Permutations
- Permutations II
- Longest Palindrome
- Valid Palindrome
- Longest Palindromic Substring
- Closest Number in Sorted Array
- Last Position of Target
- Search a 2D Matrix
- Maximum Number in Mountain Sequence
- Search in a Big Sorted Array
- Find Minimum in Rotated Sorted Array
- Find Peak Element
- First Bad Version
- Search in Rotated Sorted Array
- Total Occurrence of Target
- Drop Eggs
- First Position of Target
- K Closest Numbers In Sorted Array
- Divide Two Integers
- Search for a Range
- Search a 2D Matrix II
- Smallest Rectangle Enclosing Black Pixels
- Sqrt(x)
- Maximum Average Subarray
- Find Minimum in Rotated Sorted Array II
- Search in Rotated Sorted Array II
- Copy Books
- Wood Cut
- Pow(x, n)
- Fast Power
- Minimum Depth of Binary Tree with Maximum Average
- Minimum Subtree
- Binary Tree Paths
- Flatten Binary Tree to Linked List
- Maximum Depth of Binary Tree
- Binary Tree Preorder Traversal
- Balanced Binary Tree
- Binary Tree Inorder Traversal
- Binary Tree Postorder Traversal
- Validate Binary Search Tree
- Binary Tree Longest Consecutive Sequence
- Binary Tree Longest Consecutive Sequence II
- Binary Tree Longest Consecutive Sequence III
- Binary Tree Path Sum
- Binary Tree Path Sum II
- Binary Tree Path Sum III
- Binary Tree Maximum Path Sum II
- Inorder Successor in Binary Search Tree
- Convert Binary Search Tree to Doubly Linked List
- Minimum Depth of Binary Tree
- Lowest Common Ancestor
- Lowest Common Ancestor II
- Lowest Common Ancestor III
- Number of Islands
- Number of Islands II
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Course Schedule
- Course Schedule II
- Search Graph Nodes
- Knight Shortest Path
- Knight Shortest Path II
- Zombie in Matrix
- Graph Valid Tree
- Clone Graph
- Binary Tree Serialization
- Build Post Office
- Build Post Office II
- Convert Binary Tree to Linked Lists by Depth
- Remove Substrings
- Sequence Reconstruction
- Six Degrees
- Topological Sorting
- Word Ladder
- Connected Component in Undirected Graph
- Binary Tree Zigzag Level Order Traversal
- Smallest Rectangle Enclosing Black Pixels
- Palindrome Partitioning
- Combination Sum
- Combination Sum II
- Subsets II
- Permutations
- Permutations II
- Word Ladder
- Word Ladder II
- Next Permutation
- Next Permutation II
- Previous Permutation
- Palindrome Partitioning II
- Word Break
- Word Break II
- String Permutation
- String Permutation II
- Permutation Index
- Permutation Index II
- Insert into a Cyclic Sorted List
- Merge Two Sorted Lists
- Subarray Sum
- Maximum Subarray
- Maximum Subarray II
- Maximum Subarray III
- Maximum Subarray IV
- Maximum Subarray V
- Subarray Sum Closest
- Copy List with Random Pointer
- Linked List Cycle
- Linked List Cycle II
- Sort List
- Reverse Nodes in k-Group
- Median of two Sorted Arrays
- Intersection of Two Arrays
- Intersection of Two Arrays II
- Partition List
- Merge Sorted Array
- Merge Two Sorted Arrays
- Maximum Average Subarray
- Maximum Product Subarray
- Maximum Subarray Difference
- Two Sum - Data structure design
- Two Sum
- Two Sum - Input array is sorted
- Two Sum - Less than or equal to target
- Two Sum - Unique pairs
- Two Sum - Closest to target
- Two Sum - Difference equals to target
- Two Sum - Greater than target
- Two Sum III - Data structure design
- 3Sum
- 3Sum Closest
- 4Sum
- Remove Duplicate Numbers in Array
- Sort Colors
- Sort Colors II
- Partition Array
- Partition Array II
- Window Sum
- Move Zeroes
- Valid Palindrome
- Valid Palindrome II
- Kth Smallest Numbers in Unsorted Array
- Triangle Count
- Middle of Linked List
- Sort Integers
- Sort Integers II
- Kth Largest Element
- Pancake Sorting
- Intersection of Two Linked Lists
- Linked List Cycle
- Linked List Cycle II
- Hash Function
- High Five
- K Closest Points
- Kth Largest Element II
- Top k Largest Numbers
- Top k Largest Numbers II
- Rehashing
- Merge k Sorted Lists
- Ugly Number
- Ugly Number II
- strStr II
- LRU Cache
- LFU Cache
- Flatten 2D Vector
- Merge k Sorted Arrays
- Top K Frequent Words
- Top K Frequent Words(Map Reduce)
- Top K Frequent Words II
- Heapify
- Longest Consecutive Sequence
- Nested List Weight Sum
- Nested List Weight Sum II
- Implement Stack by Two Queues
- Expression Expand
- Zigzag Iterator
- Zigzag Iterator II
- Flatten Nested List Iterator
- Unique Paths
- Unique Paths II
- Climbing Stairs
- Climbing Stairs II
- Minimum Path Sum
- Triangle
- Largest Divisible Subset
- Knight Shortest Path
- Knight Shortest Path II
- Perfect Squares
- Jump Game
- Jump Game II
- Longest Increasing Subsequence
- Russian Doll Envelopes
- Frog Jump
- Drop Eggs
- Drop Eggs II
- Kth Smallest Number in Sorted Matrix
- Minimum Size Subarray Sum
- Longest Substring Without Repeating Characters
- Longest Substring with At Most K Distinct Characters
- Kth Smallest Sum In Two Sorted Arrays
- Kth Largest in N Arrays
- Two Sum - Less than or equal to target
- Kth Smallest Numbers in Unsorted Array
- Triangle Count
- Minimum Window Substring
- Kth Largest Element
- Number of Islands
- Number of Islands II
- Connecting Graph
- Connecting Graph II
- Connecting Graph III
- Graph Valid Tree
- Add and Search Word
- Implement Trie
- Find the Weak Connected Component in the Directed Graph
- Connected Component in Undirected Graph
- Word Search
- Word Search II
- Surrounded Regions
- Boggle Game
- Word Squares
- Kth Smallest Sum In Two Sorted Arrays
- Count of Smaller Number before itself
- Interval Sum
- Interval Sum II
- Expression Expand
- Trapping Rain Water
- Trapping Rain Water II
- Implement Queue by Two Stacks
- Min Stack
- Sliding Window Median
- Maximal Rectangle
- Max Tree
- Largest Rectangle in Histogram
- Data Stream Median
- Sliding Window Maximum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum II
- Heapify
- K Edit Distance
- Maximal Rectangle
- Expression Tree Build
- Convert Expression to Reverse Polish Notation
- Convert Expression to Polish Notation
- Expression Evaluation
- The Skyline Problem
- Sqrt(x)
- Sqrt(x) II
- Maximum Average Subarray II
- Number of Airplanes in the Sky
- Divide Two Integers
- Find Peak Element
- Find Peak Element II
- First Bad Version
- Copy Books
- Wood Cut
- Find the Duplicate Number
- Smallest Rectangle Enclosing Black Pixels
- Building Outline
- Longest Increasing Continuous Subsequence
- Longest Increasing Continuous subsequence II
- Maximum Subarray
- Maximal Square
- Maximal Square II
- Longest Palindromic Substring
- Coins in a Line
- Coins in a Line II
- Coins in a Line III
- House Robber
- House Robber II
- House Robber III
- Maximum Product Subarray
- Longest Increasing Subsequence
- Copy Books
- Copy Books II
- Longest Repeating Subsequence
- Post Office Problem
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Best Time to Buy and Sell Stock with Cooldown
- Best Time to Buy and Sell Stock with Transaction Fee
- Stone Game
- Stone Game II
- Edit Distance
- K Edit Distance
- Backpack
- Backpack II
- Backpack III
- Backpack IV
- Backpack V
- Backpack VI
- Longest Common Subsequence
- Scramble String
- Distinct Subsequences
- Minimum Adjustment Cost
- Interleaving String
- Burst Balloons
- k Sum
- k Sum II
- Coins in a Line
- Coins in a Line II
- Coins in a Line III
- Combination Sum IV
- Flatten Binary Tree to Linked List
- Flatten List
- Subarray Sum
- Subarray Sum II
- Flatten Nested List Iterator
- Submatrix Sum
- Continuous Subarray Sum
- Continuous Subarray Sum II
- Kth Smallest Number in Sorted Matrix
- Minimum Size Subarray Sum
- Subarray Sum Closest
- Find Peak Element
- Find Peak Element II
- Sliding Window Matrix Maximum
- Maximum Gap
- Nested List Weight Sum
- Flatten 2D Vector
- Bomb Enemy
- Zigzag Iterator
- Zigzag Iterator II
- Build Post Office
- Build Post Office II
- #1. Tow Sum &&ruby &&go
- #2. Add Two Numbers
- #21. Merge Two Sorted Lists &&ruby
- #26. Remove Duplicates from Sorted Array
- #77. Combinations
- #111. Minimum Depth of Binary Tree
- #107. Binary Tree Level Order Traversal II
- #141. Linked List Cycle
- #142. Linked List Cycle II
- #209. Minimum Size Subarray Sum
- #287. Find the Duplicate Number
- #345. Reverse Vowels of a String
- #409. Longest Palindrome
-
OlogN
-
start + (end - start) / 2
- pre order
- in order
- post order
- 优化
- 重复计算 无效计算
- 深搜中出栈的那一步
- window 同向双指针
- 数据流
- 记忆化搜索
- 子问题有交集
- 状态定义 => 初始化 => 循环递推求解 => 求结果:起点
- 题型: 最值,判断可行性,统计方案个数
- 2^n n! => n^2 n^3
for (int i = 0; i < n; i++) {
while(j < n) {
if(满足条件) {
j++;
更新j状态
} else(不满足条件) {
break;
}
更新i状态
}
}
- LIFO
public class Stack {
private int[] data;
private int size;
private int top = 0; // 指向栈的顶部
public Stack(int size) {
this.size = size;
this.data = new int[size];
}
}
- FIFO
- Middle of the Linked List
- Reverse Linked List
- Merge Two Sorted Lists
- Linked List Cycle
- Delete Node in a Linked List
- 注意点
- 如果链表为空时,代码是否能正常工作?
- 如果链表只包含一个结点时,代码是否能正常工作?
- 如果链表只包含两个结点时,代码是否能正常工作?
- 代码逻辑在处理头结点和尾结点的时候,是否能正常工作?
class LinkNode {
public int data;
public LinkNode next;
public LinkNode(int data) {
this.data = data;
this.next = null;
}
}
class ListNode
attr_accessor :val, :next
def initialize(val)
@val = val
@next = nil
end
end
head = ListNode.new(1)
head.next = ListNode.new(2)
head.next.next = ListNode.new(3)
head.next.next.next = ListNode.new(4)
head.next.next.next.next = ListNode.new(5)
-
O(size of key)
-
用来查重
-
替代: BBST => TreeSet,trie,BloomFilter
-
LFU
-
LRU
class Node {
public Object data;
public Node left;
public Node right;
}
- vs PriorityQueue
TreeMap | Heap | PriorityQueue | |
---|---|---|---|
1. Insert() | logn | logn | logn |
2. Delete() | logn | logn | O(n) / X |
3. Pop() | logn | logn | logn |
4. Find() | logn | logn | X |
5. Modify() | logn | logn | X |
6. Min / Max | logn | O(1) | O(1) |
7. upper / lower | logn | X | X |
class UnionFind{
HashMap<Integer, Integer> father = new HashMap<Integer, Integer>();
UnionFind(int n){
for(int i = 0 ; i < n; i++) {
father.put(i, i);
}
}
int compressed_find(int x){
int parent = father.get(x);
while(parent!=father.get(parent)) {
parent = father.get(parent);
}
int temp = -1;
int fa = father.get(x);
while(fa!=father.get(fa)) {
temp = father.get(fa);
father.put(fa, parent) ;
fa = temp;
}
return parent;
}
void union(int x, int y){
int fa_x = compressed_find(x);
int fa_y = compressed_find(y);
if(fa_x != fa_y)
father.put(fa_x, fa_y);
}
}
public class UnionFind {
private int[] father = null;
public int find(int x) {
if (father[x] == x) {
return x;
}
return father[x] = find(father[x]);
}
public void union(int a, int b) {
int root_a = find(a);
int root_b = find(b);
if (root_a != root_b) {
father[root_a] = root_b;
}
}
}
class TrieNode {
public TrieNode[] children;
public boolean isWord;
public TrieNode() {
children = new TrieNode[26];
for (int i = 0; i < 26; ++i) {
children[i] = null;
}
isWord = false;
}
}
private TrieNode root;
public WordDictionary(){
root = new TrieNode();
}
public void addWord(String word) {
// write your code here
TrieNode current = root;
for(int i = 0; i < word.length(); i++) {
Character c = word.charAt(i);
if (current.children[c - 'a'] == null) {
current.children[c - 'a'] = new TrieNode();
}
current = current.children[c - 'a'];
}
current.isWord = true;
}
boolean find(String word, int index, TrieNode now) {
if(index == word.length()) {
return now.isWord;
}
Character c = word.charAt(index);
if (c == '.') {
for(int i = 0; i < 26; ++i)
if (now.children[i] != null) {
if (find(word, index+1, now.children[i]))
return true;
}
return false;
} else if (now.children[c - 'a'] != null) {
return find(word, index+1, now.children[c - 'a']);
} else {
return false;
}
}
- 和Hash比更加节约空间
- 可用来优化某些dfs
- state, function, intialization, result
- 记忆化搜索来达到减枝的目的
- 矩阵
- 记忆化搜索
- 状态转移特别麻烦,不是顺序性
- 初始化状态不好找
- 从大到小
- 博弈类
- 画出搜索树,只考虑先手策略
- 空间类
- crack and code inetrview