- KMP algorithm
- 28 Find the Index of the First Occurrence in a String
- Boyer–Moore majority vote algorithm
- 229 Majority Element II
- Record in-place
- 41 First Missing Positive
- Two Pointers
- 42 Trapping Rain Water
- Fast slow pointer
-
- Convert Sorted List to Binary Search Tree
-
- Linked List Cycle
-
- Linked List Cycle II
-
- Palindrome Linked List
-
- Reservoir sampling
-
- Linked List Random Node
-
- Using stack to find the next element
-
- Binary Search Tree Iterator
-
- Lowest Common Ancestor (LCA)
-
- Lowest Common Ancestor of a Binary Search Tree
-
- In-Order Traversal
-
- Find Mode in Binary Search Tree
-
- Minimum Absolute Difference in BST
-
- Convert BST to Greater Tree
-
- Two Sum IV - Input is a BST (Stack + Inorder Traversal)
-
- Increasing Order Search Tree
-
- Balance a Binary Search Tree
-
- Pre-Order Traversal
-
- Serialize and Deserialize BST
-
- Construct Binary Search Tree from Preorder Traversal
-
- Euler Path / Cycle
-
- Reconstruct Itinerary
-
- Topological Order
- 210 Course Schedule II
- Cycle detection
- 207 Course Schedule
- Backpack Problem (select / not select)
sort(
vec.begin(),
vec.end(),
[&](const A& a, const A& b) {return a > b}
);
auto comp = [&](ListNode* a, ListNode* b){return (a->val) > (b->val);};
priority_queue<ListNode*, vector<ListNode*>, decltype(comp)> pq(comp);
priority queue template
ListNode*
: Type of elementsvector<ListNode*>
: Type of containerdecltype(comp)
: Type of comparing function. Usedecltype
to know the type of comparing function.
priority queue constructor
pq(comp)
: comp, the comparing function as the parameter of the constructor of the priority queue.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
auto it = upper_bound(vec.begin(), vec.end(), element);
auto it = lower_bound(vec.begin(), vec.end(), element);
#include <string>
#include <vector>
#include <stream>
usingn namespace std;
std::stringstream test("this_is_a_test_string");
std::string segment;
std::vector<std::string> seglist;
while(std::getline(test, segment, '_'))
{
seglist.push_back(segment);
}
- Remember the typecast
struct ListNode* ptr = (struct ListNode *)malloc(sizeof(struct ListNode));
char *a;
int len_a = strlen(a);
/* a[len_a] is '\0' */