2020年9月6日 星期日

LeetCode 107. Binary Tree Level Order Traversal II [Easy] [C++] 解題筆記

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its bottom-up level order traversal as:

[
  [15,7],
  [9,20],
  [3]
]

2020年9月5日 星期六

LeetCode 104. Maximum Depth of Binary Tree [Easy] [C++] 解題筆記

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its depth = 3.

LeetCode 103. Binary Tree Zigzag Level Order Traversal [Medium] [C++] 解題筆記

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its zigzag level order traversal as:

[
  [3],
  [20,9],
  [15,7]
]

2020年8月31日 星期一

LeetCode 102. Binary Tree Level Order Traversal [Medium] [C++] 解題筆記

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]

2020年8月25日 星期二

LeetCode 101. Symmetric Tree [Easy] [C++] 解題筆記

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

     1
    / \
  2   2
 / \    / \
3  4 4  3

 

But the following [1,2,2,null,3,null,3] is not:

    1
    / \
  2   2
   \    \
   3    3

2020年7月19日 星期日

LeetCode 92. Reverse Linked List II [Medium] [C++] 解題筆記

Reverse a linked list from position m to n. Do it in one-pass.

Note: 1 ≤ mn ≤ length of list.

EX:

        Input: 1->2->3->4->5->NULL, m = 2, n = 4

        Output: 1->4->3->2->5->NULL

2020年7月18日 星期六

LeetCode 53. Maximum Subarray [Easy] [C++] 解題筆記

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

EX:

        Input: [-2,1,-3,4,-1,2,1,-5,4],

        Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

LeetCode 52. N-Queens II [Hard] [C++] 解題筆記

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

Given an integer n, return the number of distinct solutions to the n-queens puzzle.

EX:

     Input: 4

    Output: 2
    Explanation: There are two distinct solutions to the 4-queens puzzle as shown below.
    [
     [".Q..",  // Solution 1
      "...Q",
      "Q...",
      "..Q."],

     ["..Q.",  // Solution 2
      "Q...",
      "...Q",
      ".Q.."]
    ]

LeetCode 51. N-Queens [Hard] [C++] 解題筆記

這題是經典的 n 皇后問題。

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.

EX:

        Input: 4

        Output: [
         [".Q..",  // Solution 1
          "...Q",
          "Q...",
          "..Q."],

         ["..Q.",  // Solution 2
          "Q...",
          "...Q",
          ".Q.."]
        ]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.

LeetCode 50. Pow(x, n) [Medium] [C++] 解題筆記

Implement pow(x, n), which calculates x raised to the power n (xn).


EX:

        Input: 2.00000, 10

        Output: 1024.00000
        Input: 2.00000, -2
        Output: 0.25000
        Explanation: 2-2 = 1/22 = 1/4 = 0.25

LeetCode 49. Group Anagrams [Medium] [C++] 解題筆記

這題給定一個 array 包含一些 string,要求我們將由同樣字元組成的 string 歸到同一個 array。

EX:

        Input: ["eat", "tea", "tan", "ate", "nat", "bat"],

        Output:
        [
          ["ate","eat","tea"],
          ["nat","tan"],
          ["bat"]
        ]

LeetCode 48. Rotate Image [Medium] [C++] 解題筆記

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

EX:

        Given input matrix

    [
      [1,2,3],
      [4,5,6],
      [7,8,9]
    ],

    rotate the input matrix in-place such that it becomes:
    [
      [7,4,1],
      [8,5,2],
      [9,6,3]
    ]

2020年7月16日 星期四

LeetCode 47. Permutations II [Medium] [C++] 解題筆記

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

EX:
        Input: [1,1,2]
    Output:
    [
      [1,1,2],
      [1,2,1],
      [2,1,1]
    ]