Skip to content

Blog

LeetCode: 18-4Sum 解題紀錄


題目

Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Notice that the solution set must not contain duplicate quadruplets.

Example:

Input: nums = [1,0,-1,0,-2,2], target = 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

這個題目是 2Sum3Sum 的衍伸版,同樣給定一個整數陣列輸入,我們要找出由四個數加總為 0 的所有組合。

Read More »LeetCode: 18-4Sum 解題紀錄

LeetCode: 32-Longest Valid Parentheses 解題紀錄


題目

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

Example:

Input: s = "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()".

Input: s = ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()".

這個題目輸入一個只由 '(' 或是 ')' 組成的字串,而我們要做的,就是判斷最長的『合理』子字串長度。

比方說, "()" 就是合理的子字串、")(" 就是個不合理的子字串。

Read More »LeetCode: 32-Longest Valid Parentheses 解題紀錄

取消 HTTP 網址重新導向 HTTPS

Cryptocurrency mining equipment, isometric ethereum digital currency extract, blockchain system dark blue vector illustration

前言

在使用 Chrome 瀏覽器時,有時當我們進入 HTTP 的網址時會自動導向 HTTPS 的網址。(不確定其他瀏覽器是否會幫忙做這項服務)

當然,能夠自動導向有憑證、加密的網址當然是很好的。但是,若是當你正在架設網站,比方說反覆測試自己部署上線的網站時,會發生網址直接轉向剛剛已經建立憑證的 HTTPS 網址,而無法再次前往再次部署上的網站。

簡單來說,這會影響到我們測試網站部署。

Read More »取消 HTTP 網址重新導向 HTTPS

LeetCode: 16-3Sum Closest 解題紀錄


題目

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

Constraints:

  • 3 <= nums.length <= 10^3
  • -10^3 <= nums[i] <= 10^3
  • -10^4 <= target <= 10^4

這個題目跟 15 題非常相似,同樣是給定一組整數陣列輸入,不過還多給了一個目標值 target。我們需要找出 3 個數值,使其加總值越接近 target 越好。

需要注意的是,我們要返回的並不是找到的陣列,而是直接返回最接近 target 的數值。

Read More »LeetCode: 16-3Sum Closest 解題紀錄

LeetCode: 15-3Sum 解題紀錄


題目

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Notice that the solution set must not contain duplicate triplets.

Example:

Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]

Input: nums = []
Output: []

題目是給定一個全是由『整數』組成的陣列,而我們的目標就是要找出『任意三個數值加總為 0 的所有答案』,也是題目 3Sum 的意義。

還有,返回的結果不能有重複的答案。

Read More »LeetCode: 15-3Sum 解題紀錄

[Linux] 升級 sudo 套件的幾個方法

Linux is an operating system that can do all the functions you imagine

前言

最近看到新聞,發現現在在 Unix-liked 系統中大家常用的 sudo 工具,原來存在著一個『堆積緩衝溢位漏洞』的 BUG(編號 CVE-2021-3156)。

目前在以下版本確認了能夠取得完整的 root 權限:

  • Ubuntu 20.04(sudo 1.8.31)
  • Debian 10(sudo 1.8.27)
  • Fedora 33(sudo 1.9.2)

而據報導、以及我粗略看過的幾個討論區,幾乎都在建議大家迅速升級該套件版本。這個 BUG 在 2021/01 月中發現而通報 sudo 開發單位,差不多一個禮拜多,新版的修復就已經上線了。

以下我簡單紀錄該如何升級 sudo 版本。

Read More »[Linux] 升級 sudo 套件的幾個方法

LeetCode: 31-Next Permutation 解題紀錄


題目

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).

The replacement must be in place and use only constant extra memory.

Example:

Input: nums = [1,2,3]
Output: [1,3,2]

Input: nums = [3,2,1]
Output: [1,2,3]

Input: nums = [1,1,5]
Output: [1,5,1]

Input: nums = [1]
Output: [1]

題目給定輸入一個陣列,我們要將陣列排列成『下一個較大的值』。比方說題目輸入了 [1,2,3],那麼我們就不能排列成 [3,1,2]、而是要排列成 [1,3,2] —— 因為 132 才下一個較大的值,而非 312。

而當題目給定的陣列沒有下一個較大的排列時,則將陣列『從小排到大』。

順帶一提這題不會返回任何值,直接處理輸入陣列即可。

Read More »LeetCode: 31-Next Permutation 解題紀錄