My code can be viewed in my github: Proverbs Github.
The following difficulty is defined by myself.

all-nodes-distance-k-in-binary-tree(medium)

ALL problem related to distance in trees can be solved using depth.
Find the depth of the target node. If it is in the left subtree, find all nodes which meet the requirement in the right subtree. And if it is in the right subtree, find all nodes in left subtree.
If the difference of depth between current node and the target node is K, push back current node to the results.

  • Time complexity: O(n).
  • Space complexity: O(n).

binary-tree-paths(easy)

DFS.

  • Time complexity: O(n).
  • Space complexity: O(n).

diagonal-traverse(easy)

There are 4 ways of going out of bounds in total.

  • Time complexity: O(row*col).
  • Space complexity: O(row*col).

encode-and-decode-strings(medium)

Because all 256 characters can be used, so we cannot use special characters as separator. A smart way is to combine the length with the string to encoded.

  • Time complexity: O(#strings*length).
  • Space complexity: O(#strings*length).

flood-fill(easy)

DFS. Be careful of the case that the new color is the same as current color.

  • Time complexity: O(row*col).
  • Space complexity: O(row*col).

largest-number(medium)

It is quite typical problem.
We can use a custom comparator to decide how to order the numbers.

  • Time complexity: O(nlogn*length).
  • Space complexity: O(n*length).

longest-continuous-increasing-subsequence(easy)

Scan from left to right.

  • Time complexity: O(n).
  • Space complexity: O(1).

maximum-sum-circular-subarray(hard)

I worked out a method of using two pointers. However, it showed that this method is wrong although I did not find a small-scale counter-example.
Then I use sliding window and keep track of the minimum number in the window by converting sums of subarray to difference between prefix sums.

  • Time complexity: O(n).
  • Space complexity: O(n).

middle-of-the-linked-list(easy)

The straightforward way is to find the length.
Another way is to use fast-slow pointers.

  • Time complexity: O(n).
  • Space complexity: O(1).

minimum-number-of-refueling-stops(medium)

DP. dp[i][j]: max fuel when the car reaches i and has stopped at j stations for refueling. There are two ways of state transition: refuel at i or not refuel.
And because the target may not be a station, so we create a virtual station for convenience.
Optimization: first dimension can be compressed or even eliminated.

  • Time complexity: O(n^2).
  • Space complexity: O(n^2) or O(n).

next-greater-element-iii(medium)

Next permutation.
Find the longest ascending sequence from right to left. Then, swap and reverse.
Just be careful of the case that there exists many digits with the same value.

  • Time complexity: O(n).
  • Space complexity: O(1).

number-of-1-bits(easy)

Bits operation.

  • Time complexity: O(32).
  • Space complexity: O(1).

number-of-islands-ii(medium)

Disjoint set union. This problem can be solved online.

  • Time complexity: O(row*col) if considering findfa() as an O(1) method.
  • Space complexity: O(row*col).

number-of-music-playlists(hard)

DP. dp[i][j]: number of ways to play first i songs using j unique songs. There are two ways of state transition: play a new song or an old song.

  • Time complexity: O(NL).
  • Space complexity: O(NL).

peeking-iterator(medium)

At first glance, I did not have any idea of how to do it.
And after a while, I use a variable to keep track of whether peek() function has been called. If true, we will not call Iterator::next() for the next next() function.

  • Time complexity: O(1).
  • Space complexity: O(1).

reorganize-string(medium)

The straight forward method is to first meet the requirement from the most frequent number.

  • Time complexity: O(nlogn).
  • Space complexity: O(n).

smallest-range(medium)

The same as merge k ordered arrays.

  • Time complexity: O(nlogk).
  • Space complexity: O(k).

sum-of-two-integers(easy)

Add using 1’s complement.

  • Time complexity: O(32).
  • Space complexity: O(1).

toeplitz-matrix(easy)

  • Time complexity: O(row*col).
  • Space complexity: O(1).

transpose-matrix(easy)

  • Time complexity: O(row*col).
  • Space complexity: O(row*col).