Skip to content

Instantly share code, notes, and snippets.

@r2dev
Last active September 2, 2020 16:31
Show Gist options
  • Save r2dev/093728f1ef6f7ded3da3e0b4e03c90fb to your computer and use it in GitHub Desktop.
Save r2dev/093728f1ef6f7ded3da3e0b4e03c90fb to your computer and use it in GitHub Desktop.
leetcode

Majority Element - 169

solution 1: sort and choose the middle one

solution 2: hash map

solution 3: voting, lets assume the first is the right candidate, if we see zero count, we assign the candidate, if we see the vote is toward the current canadidate, we add vote count by one, if we see different canadidates, we deduct the value by one. The right answer is the remaining canadidate

Merge Two Binary Trees - 617

solution 1: recursive, if both are null, we return null, if either one of the parameters are existed, we return the one remaining. if they are both existed, we make a new node with the sum of the two parameters' node, and call the main function with theirs lefts and rights

Counting Bits - 338

solution 1: check the last binary digit by do

nums & 1

if the result is equal to 1, which mean it was 0 so the result was the previous value added by 1.

if the result is equal to 0, which mean it was shift left by 1 bits, so the value is the same value as the value when you shift the value right by 1 bits

Maximum Depth of binary tree - 104

solution 1: recursive

if the parameter is null, we return 0 - end condition

otherwise, we add the max value between paramter.left's max depth and right's node max depth by 1

solution 2: level base count

we create a queue, while the queue is not empty, we are counting (by add 1). we grab every elements in one level and push to the array. delete the previous level node.

for loop in a while loop. we can delete the previous level node by step in the for loop, or we can delete them all together in one go after for loop

Single number - 136

  1. we use xor, if duplicate, it will become 0

  2. math, the double sum of the unique number minus the total value

  3. hash map

Invert binary tree - 226

  1. recursive: revert the result of the left node and right node
  2. customize stack: create a stack, push left and right to the stack, pop and resolve until stack is empty
  3. queue: same as above, just use queue instead of stack.

reverse Linked list - 206

  1. iteratively: iterate through the list, on each iteration, build the result with new listNode with current cursor value and previous result.
  2. iteratively and replace value in-place
  3. recursively: create a recursive function which takes current node and result as parameters, if current node is null, we return result. if its not null, we form and produce a new listNode with current cursor's value and result as the next node. (in-place is possible, save the right next to a variable)

Best time to buy and sell stock - 121

  1. brute force

  2. one pass loop, we track the minimum value and the maximum profit

    when we see a minimum value appears, we assign it to minimum value, make it a protential day to buy in, otherwise we calculate the profit and see if this is the maximum profit day to sell.

Move Zeroes - 283

  1. count zero and delete them, loop and push the zero in the end, attention: when you remove them, the length of the array also change
  2. we track lastNonZeroIndex, loop through the array, if we see non-zero we assign the current value to the lastNonZeroIndex At and increase it by one. then we append zero at the end
  3. same logic but we swap, maybe good in c++

Find All numbers disappeared in an array

convert the number to index. make the indexed number negative to track if the number in the array is existed. use the array as a hashmap. stupid and smart....

merge Two sorted lists - 21

  1. loop, advance one of the list when its current value is less. Javascript, create a dummy ListNode to hold the value
  2. recurisve

Diameter of Binary tree - 543

  1. DFS, the max length which passes through this node is the max diameter value of left plus the right. but the depth for the current node is the max diameter value between left and right plus one

Climbing stairs

  1. dynamic programming, its either by using array or by passing addition state parameter into function
  2. dynaimc programming without array, use two variable to save the only two previous value if needed

Symmetric tree - 101

  1. recursive, create a child function which take two node and compare them
  2. queue, push and pull two value each time.

two sum - 1

  1. one pass hashmap. we search the other value each time we iterate the value in the array. using hashmap. care. duplicated value

maximum subarray - 53

  1. dynamic programming, subquestion

House Robber - 198

1.dynamic programming

Linked list cycle - 141

  1. hash map. use set in javascript
  2. two pointer, a and b, a advanced one b advance two

Intersection of two linked lists - 160

  1. attach B to the end of headA, attach A to the end of headB, do a while loop when they are not curA and curB are not equal

Valid parenttheses - 20

  1. using stack push when open, check the match when close. make sure the stack is empty at the end

Palindrome Linked List - 234

  1. two pointer, slow advance 1 every time. fast advance 2 every time. create the first half array. iterate two array in parallel and compare the value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment