Skip to content

DennyKuo0809/leetcode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Array and String

  • 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

Linked List

  • Fast slow pointer
      1. Convert Sorted List to Binary Search Tree
      1. Linked List Cycle
      1. Linked List Cycle II
      1. Palindrome Linked List
  • Reservoir sampling
      1. Linked List Random Node

Binary Search Tree

  • Using stack to find the next element
      1. Binary Search Tree Iterator
  • Lowest Common Ancestor (LCA)
      1. Lowest Common Ancestor of a Binary Search Tree
  • In-Order Traversal
      1. Find Mode in Binary Search Tree
      1. Minimum Absolute Difference in BST
      1. Convert BST to Greater Tree
      1. Two Sum IV - Input is a BST (Stack + Inorder Traversal)
      1. Increasing Order Search Tree
      1. Balance a Binary Search Tree
  • Pre-Order Traversal
      1. Serialize and Deserialize BST
      1. Construct Binary Search Tree from Preorder Traversal

Graph

  • Euler Path / Cycle
      1. Reconstruct Itinerary
  • Topological Order
    • 210 Course Schedule II
  • Cycle detection
  • 207 Course Schedule

DP

  • Backpack Problem (select / not select)

C++ cheat sheet

lambda function for comparing

sort(
  vec.begin(),
  vec.end(),
  [&](const A& a, const A& b) {return a > b}
);

lambda comparing function for the priority queue

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 elements
  • vector<ListNode*>: Type of container
  • decltype(comp): Type of comparing function. Use decltype 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.

constructor in structures

/**
 * 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) {}
 * };
 */

upper_bound, lower_bound

auto it = upper_bound(vec.begin(), vec.end(), element);
auto it = lower_bound(vec.begin(), vec.end(), element);

Slice a string by a specific character

#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);
}

C cheat sheet

malloc

  • Remember the typecast
struct ListNode* ptr = (struct ListNode *)malloc(sizeof(struct ListNode));

strlen

char *a;
int len_a = strlen(a);
/* a[len_a] is '\0' */

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published