2020年7月10日 星期五

LeetCode 30. Substring with Concatenation of All Words [Hard] [C++] 解題筆記

這題給我們一個 string s 和一些等長 words,要我們求出 s 中所有可以由所有 words 組合形成的子串。

EX:
        Input:
        s = "barfoothefoobarman",
        words = ["foo","bar"]
        Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.

2020年7月9日 星期四

LeetCode 29. Divide Two Integers [Medium] [C++] 解題筆記

Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.

Return the quotient after dividing dividend by divisor.

The integer division should truncate toward zero, which means losing its fractional part. For example, truncate(8.345) = 8 and truncate(-2.7335) = -2.


EX:

        Input: dividend = 7, divisor = -3

        Output: -2

        Explanation: 7/-3 = truncate(-2.33333..) = -2.

2020年7月8日 星期三

LeetCode 28. Implement strStr() [Easy] [C++] 解題筆記

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

EX:

        Input: haystack = "hello", needle = "ll"

        Output: 2
        Input: haystack = "aaaaa", needle = "bba"
        Output: -1

LeetCode 27. Remove Element [Easy] [C++] 解題筆記

Given an array nums and a value val, remove all instances of that value in-place and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.


EX:

        Given nums = [0,1,2,2,3,0,4,2], val = 2,

    Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
    Note that the order of those five elements can be arbitrary.
    It doesn't matter what values are set beyond the returned length.

LeetCode 26. Remove Duplicates from Sorted Array [Easy] [C++] 解題筆記

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.


EX:

        Given nums = [0,0,1,1,1,2,2,3,3,4],

    Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
    It doesn't matter what values are set beyond the returned length.        

2020年7月7日 星期二

LeetCode 25. Reverse Nodes in k-Group [Hard] [C++] 解題筆記

這題給定我們一個 linked list,與一個值 k ,要求我們以每 k 個 node 為一組 reverse 此 list。

EX:
        Given this linked list: 1->2->3->4->5

        For k = 2, you should return: 2->1->4->3->5

        For k = 3, you should return: 3->2->1->4->5

LeetCode 24. Swap Nodes in Pairs [Medium] [C++] 解題筆記

Given a linked list, swap every two adjacent nodes and return its head.

You may not modify the values in the list's nodes, only nodes itself may be changed.

EX:

        Given 1->2->3->4, you should return the list as 2->1->4->3.

2020年7月5日 星期日

LeetCode 23. Merge k Sorted Lists [Hard] [C++] 解題筆記

這題給定 k 個排序好的 linked list,要求將這 k 個 list 合併成一個排序好的 linked list。

EX:
        Input:
    [
      1->4->5,
      1->3->4,
      2->6
    ]
    Output: 1->1->2->3->4->4->5->6

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"].