2020年6月26日 星期五

LeetCode 22. Generate Parentheses [Medium] [C++] 解題筆記

這題給定 n 組括弧,要我們求出 n 組括弧所能形成的所有合法組合。

EX:
        n = 3,

    [
      "((()))",
      "(()())",
      "(())()",
      "()(())",
      "()()()"
    ]

2020年6月23日 星期二

LeetCode 21. Merge Two Sorted Lists [Easy] [C++] 解題筆記

這題給定兩個排序好的 Linked List,要我們將兩個 list 拼接成一個排序好的 list。

EX:
        Input: 1->2->4, 1->3->4
       Output: 1->1->2->3->4->4

LeetCode 20. Valid Parentheses [Easy] [C++] 解題筆記

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

EX:

        Input: "()[]{}"

       Output: true

        Input: "([)]"
       Output: false

LeetCode 19. Remove Nth Node From End of List [Medium] [C++] 解題筆記

Given a linked list, remove the n-th node from the end of list and return its head.

EX:

        Given linked list: 1->2->3->4->5, and n = 2.

    After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:

Given n will always be valid.

2020年6月22日 星期一

LeetCode 17. Letter Combinations of a Phone Number [Medium] [C++] 解題筆記

給定一個由 2-9組成的字串 s,其中 2-9 分別可以對應到幾個特定字母如下圖:


題目要求我們找出 s 可以對應到的所有字母組合。

EX:
        Input: "23"
        Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

2020年6月21日 星期日

LeetCode 18. 4Sum [Medium] [C++] 解題筆記

這題給定一個 array,和一個 target value,要我們在其中找出四個數字且其和為 target value 的所有
組合。並要求不可以有重複的解。

EX:
      nums = [1, 0, -1, 0, -2, 2] , target = 0.
   Ans:
    [
      [-1,  0, 0, 1],
      [-2, -1, 1, 2],
      [-2,  0, 0, 2]
    ]

LeetCode 16. 3Sum Closest [Medium] [C++] 解題筆記

這題給定一個 array 和一個 target value,要我們在 array 中找三個數其和最接近 target value。並且題目保證只有唯一一組解。

EX:
        nums = [-1,2,1,-4], target = 1

Ans: 2,  Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

LeetCode 15. 3Sum [Medium] [C++] 解題筆記

這題給定一個 array,要我們在其中找出三個數字且其和為 0 的所有組合。

EX:
        nums = [-1, 0, 1, 2, -1, -4]
          [
Ans:     [-1, 0, 1],
            [-1, -1, 2]
          ] 

2020年6月20日 星期六

LeetCode 14. Longest Common Prefix [Easy] [C++] 解題筆記

這題給我們一些字串,要我們找出這些字串的最長 prefix string。

EX:
        Input: ["flower","flow","flight"]
    Output: "fl"
    
    Input: ["dog","racecar","car"]
    Output: ""
    Explanation: There is no common prefix among the input strings.

LeetCode 13. Roman to Integer [Easy] [C++] 解題筆記

    這題題目太冗了,可以直接參考 LeetCode 13. Roman to Integer。簡而言之就是給我們一個羅馬數字表示的字串,要我們轉換成對應的整數值。
Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

EX:
Input: "III"
Output: 3
Input: "IV"
Output: 4
Input: "IX"
Output: 9
Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

LeetCode 11. Container With Most Water [Medium] [C++] 解題筆記

這題題目解釋太複雜,直接看下圖,簡而言之,這題要我們找兩條 bar 其組成的容器容積最大。

EX:
    Input: [1,8,6,2,5,4,8,3,7]
    Output: 49

LeetCode 10. Regular Expression Matching [Hard] [C++] 解題筆記

這題給定一個 string (s) 與一個 pattern (p) ,要求我們實作支援 ' . ' 與 ' * ' 操作的正規表示法。

'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

Note:

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like . or *.

2020年6月18日 星期四

LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal [Medium] [C++] 解題筆記

這題給我們一個 binary tree 的中序和前序排序,要我們反推出整個 tree 的結構,其中 tree 的每個node 皆不重複。

EX:
        preorder = [3,9,20,15,7]
        inorder = [9,3,15,20,7]
                3
               / \
  Ans:    9  20
              /  \
           15   7