leetcode today 6

LC 206, 21, 23, 88, 127, 139, 204, 232, 346, 496, 503

206. Reverse Linked List

  • Reverse the pointer to point the previous element, both in iteration and recursion
  • Linkedlist

21. Merge Two Sorted Lists

  • Just like merge sort, both in iteration and recursion
  • Merge sort, Linkedlist

23. Merge k Sorted Lists

  • Use priority queue

88. Merge Sorted Array

  • Merge from the end and just like merge sort
  • Array, Merge sort

127. Word Ladder

  • The BFS queue stores all the possible strings; remember to delete from the set or use visited array to mark the visited strings
  • BFS

139. Word Break

  • Only when the substring(j, i) is in wordDict and dp[j] = true, set dp[i] = true
  • dp

204. Count Primes

  • Mark all the possible product i * j as true
  • Math, DP

232. Implement Queue using Stacks

  • Pop twice to make sure the sequence is the same
  • stack, queue

346. Moving Average from Data Stream

  • Use queue to store the number, use a variable to track the sum
  • Queue

496. Next Greater Element I

  • Use a stack to record the element in descending order
  • Stack

503. Next Greater Element II

  • Tranverse the array twice, but the second time not push in the stack
  • Stack