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

2020年6月17日 星期三

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

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

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

LeetCode 9. Palindrome Number [Easy] [C++] 解題筆記

這題要我們判斷一個整數是否是回文。

EX:
        Input: 121
        Output: true
        Input: -121
        Output: false
        Explanation: From left to right, it reads -121. From right to left, it becomes 121-. 
                             Therefore it is not a palindrome.

LeetCode 8. String to Integer (atoi) [Medium] [C++] 解題筆記

這題要我們實作一個將 string 換成 integer 的函數,類似 atoi 的功能。

EX:

        Input: " -42"

       Output: -42
       Explanation: The first non-whitespace character is '-', which is the minus sign.
                             Then take as many numerical digits as possible, which gets 42.

2020年6月16日 星期二

LeetCode 7. Reverse Integer [Easy] [C++] 解題筆記

題目: Given a 32-bit signed integer  x ,  reverse digits of an integer.

EX:
    Input: 123
    Output: 321

LeetCode 5. Longest Palindromic Substring [Medium] [C++] 解題筆記

這題給定一個字串 s ,要我們找到 s 的最長回文子串。

EX:
    Input: "babad"
    Output: "bab"
    Note: "aba" is also a valid answer.