Matrix

Spiral Matrix

  • Use dir to indicate direction 0,1,2,3
  • Use rLow, rHigh and cLow, cHigh to mark range

Pascal's triangle II

  • Create an array of size n and set arr[0] = 1
  • We use the same array to calculate each row
  • for each row, we start from the last element
  • arr[j] += arr[j-1];
    1        arr = [1,0,0,0...]
    1 1      arr = [1,1,0,0...]
    1 2 1    arr = [1,2,1,0...]
    ...
    

Minesweeper

  • Because the click position will always be an unrevealed square (M or E), we can igore the other cases.
  • If the click position is M, then we just need to change it to revealed mine X.
  • If the click position is E, we need to figure out the number of adjacent mines. There are two cases
  • First, there's more than one adjacent mine, we just need to change the square to digit
  • Second, there's no adjacent mine, then we can change the square to B. And do a DFS for all of its neighbors.
  • Since we change E to B before DFS, so we don't need to pass a visited array.

results matching ""

    No results matching ""