04 Oct 2024
問題
嘗試將包在 docker 內的 app 和 redis container 一起用 docker-compose 啟動並連接的時候,發現 app 一直連不上,而且會顯示錯誤:
[ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1085:14)
Solution
- 將 docker compose 中 redis 的部分改為下面:
``` yml
redis1:
container_name: redis1
image: redis:latest
networks:
- custom_network
command: [“redis-server”, “–bind”, “redis1”, “–port”, “6379”]
```
- app 連線 redis 的 client 部分,host 必須是 container 的名字
const redisClient1 = new Redis({
host: 'redis1',
port: 6379
});
這個解法來自這篇
20 Jul 2024
Leetcode Top Interview 150 的其中一題
題目描述
Given a 1-indexed array of integers numbers
that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1]
and numbers[index2] where 1 <= index1 < index2 <= numbers.length
.
Return the indices of the two numbers, index1
and index2
, added by one as an integer array [index1, index2]
of length 2.
The tests are generated such that there is exactly one solution. You may not use the same element twice.
Your solution must use only constant extra space.
範例
Example 1:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
Example 2:
Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].
Constraints:
- 2 <= numbers.length <= 3 * 10^4
- -1000 <= numbers[i] <= 1000
- numbers is sorted in non-decreasing order.
- -1000 <= target <= 1000
- The tests are generated such that there is exactly one solution.
解法
這題另一種解法是一邊用 map 紀錄目前有的數值,每次都檢查 target-numbers[i] 這個值是否已經存在。但由於題目規定只能使用 constant extra space,所以以下解法使用雙指針。
func twoSum(numbers []int, target int) []int {
left := 0
right := len(numbers) - 1
for left < right {
// 如果相加等於 target,回傳答案
if numbers[left] + numbers[right] == target {
return []int{left+1, right+1}
}
// 如果相加比 target 大,代表其中一個數字應該更小,將右指針左移
if numbers[left] + numbers[right] > target {
right--
}
// 如果相加比 target 小,代表其中一個數字應該更大,將左指針右移
if numbers[left] + numbers[right] < target {
left++
}
}
return []int{1, 2}
}
Time complexity : O(n)
Space complexity : O(1)
17 Jul 2024
Leetcode Top Interview 150 的其中一題
題目描述
You are given a 0-indexed array of integers nums
of length n
. You are initially positioned at nums[0]
.
Each elementnums[i]
represents the maximum length of a forward jump from index i
. In other words, if you are at nums[i]
, you can jump to any nums[i + j]
where:
0 <= j <= nums[i]
and
i + j < n
Return the minimum number of jumps to reach nums[n - 1]
. The test cases are generated such that you can reach nums[n - 1]
.
範例
Example 1:
Input: nums = [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [2,3,0,1,4]
Output: 2
Constraints:
- 1 <= nums.length <= 10^4
- 0 <= nums[i] <= 10000
- It’s guaranteed that you can reach nums[n - 1].
解法
思路:記錄從我們每一步可以到達的地方中,找出最遠下一步可以到哪
例如範例一
index 0 可以到 index 1, index 2
func jump(nums []int) int {
res := 0
currFarest := 0
start := 0
for currFarest < len(nums)-1 {
farest := 0
for i := start; i <= currFarest; i++ {
farest = max(farest, i + nums[i])
}
start = currFarest + 1
currFarest = farest
res++
}
return res
}
func max(a, b int) int {
if a > b { return a }
return b
}
Time complexity : O(n^2)
Space complexity : O(1)
13 Jul 2024
Leetcode Top Interview 150 的其中一題
題目描述
You are given an integer array nums
. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.
Return true
if you can reach the last index, or false
otherwise.
範例
Example 1:
Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
Constraints:
- 1 <= nums.length <= 10^4
- 0 <= nums[i] <= 10^5
解法
專注於我們目前能到達的最遠 index 。從前面哪個 index 到這個最遠的index並不重要。
從 index 0 開始遍歷 nums
,使用 canArrive
變數來記錄我們能到達的最遠 index。如果 i + nums[i] > canArrive
,這表示我們可以到達更遠的 index,更新canArrive
的值。如果 i < canArrive
,這表示我們無法抵達剩餘的index,返回 false。
func maximumGain(s string, x int, y int) int {
remove := func(str string, removestr string, val int) int {
count := 0
stack := make([]byte, 0)
for i:=0; i<len(str); i++ {
stack = append(stack, str[i])
l := len(stack)
if l >= 2 && stack[l-1]==removestr[1] && stack[l-2]==removestr[0] {
count += val
stack = stack[:l-2]
}
}
s = string(stack)
return count
}
if x > y {
return remove(s, "ab", x) + remove(s, "ba", y)
} else {
return remove(s, "ba", y) + remove(s, "ab", x)
}
return 0
}
Time complexity : O(n)
Space complexity : O(1)
12 Jul 2024
Leetcode Top Interview 150 的其中一題
題目描述
Given an absolute path for a Unix-style file system, which begins with a slash '/'
, transform this path into its simplified canonical path.
In Unix-style file system context, a single period '.'
signifies the current directory, a double period ".."
denotes moving up one directory level, and multiple slashes such as "//"
are interpreted as a single slash. In this problem, treat sequences of periods not covered by the previous rules (like "..."
) as valid names for files or directories.
The simplified canonical path should adhere to the following rules:
It must start with a single slash '/'
.
Directories within the path should be separated by only one slash '/'
.
It should not end with a slash '/'
, unless it’s the root directory.
It should exclude any single or double periods used to denote current or parent directories.
Return the new path.
範例
Example 1:
Input: path = "/home/"
Output: "/home"
Explanation:
The trailing slash should be removed.
Example 2:
Input: path = "/home//foo/"
Output: "/home/foo"
Explanation:
Multiple consecutive slashes are replaced by a single one.
Example 3:
Input: path = "/../"
Output: "/"
Explanation:
Going one level up from the root directory is not possible.
Constraints:
- 1 <= path.length <= 3000
- path consists of English letters, digits, period
'.'
, slash '/'
or '_'
.
- path is a valid absolute Unix path.
解法
func simplifyPath(path string) string {
if len(path) == 1 { return path }
folders := make([]string, 0)
path += "/"
bytes := make([]byte, 0)
for i:=1; i<len(path); i++ {
if path[i] != '/' {
bytes = append(bytes, path[i])
continue
}
if len(bytes) == 0 { continue }
if len(bytes) == 1 && bytes[0] == '.' {
bytes = []byte{}
continue
}
if len(bytes) == 2 && bytes[0] == '.' && bytes[1]=='.' {
if len(folders) > 0 { folders = folders[:len(folders) - 1] }
bytes = []byte{}
continue
}
folders = append(folders, string(bytes))
bytes = []byte{}
}
res := ""
for _, s := range folders {
res += "/"
res += s
}
if len(res) == 0 { return "/"}
return res
}