Tony Wu’s Blog

隨手記錄一些技術上與工作上用到與學習的程式與工具

Leetcode217 Contains Duplicate

題目 Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. 題意: 判斷陣列中是否有重複的值 解法 解法: 透過map資料結構,找出在陣列中是否有重複的值 func containsDuplicate(nums []int) bool { m := map[int]bool{} for _, v := range nums { if _, exist := m[v]; exist { return true } m[v] = true } return false } 加些測試,看看結果是不是如預期 import ( "github.com/stretchr/testify/assert" "testing" ) func Test_containsDuplicate(t *testing....

2023-05-02 May Tuesday · 1 分鐘 · Tony

Leetcode228 Summary Ranges

題目 You are given a sorted unique integer array nums. A range [a,b] is the set of all integers from a to b (inclusive). Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums....

2023-04-21 April Friday · 1 分鐘 · Tony

Leetcode290 Word Pattern

題目 Given a pattern and a string s, find if s follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s. 解法 找出pattern字串與s字串對應,並且轉換過去,且pattern對應的s不可重複,即可解答 import ( "strings" ) func wordPattern(pattern string, s string) bool { m := make(map[string]string) splitString := strings.Split(s, " ") patternString := strings.Split(pattern, "") if len(splitString) != len(patternString) { return false } for i := 0; i < len(splitString); i++ { if _, ok := m[patternString[i]]; !...

2023-04-21 April Friday · 1 分鐘 · Tony

Leetcode557 Reverse Words in a String III

題目 Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. 解法 import ( "strings" ) func reverseWords(s string) string { split := strings.Split(s, " ") res := "" for _, v := range split { res = res + reverse(v) + " " } return strings.Trim(res, " ") } func reverse(s string) string { var str string for _, v := range s { str = string(v) + str } return str } 加些測試,看看結果是不是如預期 func Test_reverseWords(t *testing....

2023-04-18 April Tuesday · 1 分鐘 · Tony

Leetcode704 Binary Search

題目 Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity. 題意: 在nums陣列中找出等於target的index值 ,並且時間複雜度須為O(log n) 解法 題目要求: 時間複雜度為 O(log n) ,所以可以使用Binary Search 方法來找出index func search(nums []int, target int) int { start := 0 end := len(nums) - 1 for start <= end { mid := start + (end-start)/2 if nums[mid] == target { return mid } else if nums[mid] > target { end = mid - 1 } else { start = mid + 1 } } return -1 } 加些測試,看看結果是不是如預期 import ( "github....

2023-04-15 April Saturday · 1 分鐘 · Tony

Leetcode35 Search Insert Position

題目 Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity. 題意: 找出相近的nums array中的index ,並且時間複雜度須為O(log n) 解法 題目要求: 時間複雜度為 O(log n) ,所以可以使用Binary Search 方法來找出index func searchInsert(nums []int, target int) int { start := 0 end := len(nums) - 1 for start <= end { mid := start + (end-start)/2 if nums[mid] == target { return mid } else if nums[mid] > target { end = mid - 1 } else { start = mid + 1 } } return start } 加些測試,看看結果是不是如預期 import ( "github....

2023-04-15 April Saturday · 1 分鐘 · Tony

MQTT Server - EMQP建立

前言 市面上MQTT Server 有很多種,例如: EMQX Mosquitto 這次我們透過docker來建立IOT傳輸用的MQTT Sever EMQX 步驟 建立一個新的folder,命名emqx 在emqx目錄底下,建立名為data資料夾,用來存放eqmx資料 在emqx目錄底下,建立docker-compose.yml docker-compose.yml 底下輸入 version: '3' services: emqx: image: emqx:5.0.20 container_name: emqx healthcheck: test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"] interval: 5s timeout: 25s retries: 5 ports: - 1883:1883 ## tcp - 8083:8083 ## websockeet - 8084:8084 ## websocket ssl - 8883:8883 ## ssl - 18083:18083 ## web volumes: - ./data:/opt/emqx/data 執行docker-compose up -d,即可啟動emqx 打開browser,在localhost:18083即可看到啟動好的emqx,如下圖 預設帳密為: admin/public 參考連結 EQMX

2023-04-08 April Saturday · 1 分鐘 · Tony

Leetcode1844 Replace All Digits with Characters

題目 You are given a 0-indexed string s that has lowercase English letters in its even indices and digits in its odd indices. There is a function shift(c, x), where c is a character and x is a digit, that returns the xth character after c. For example, shift(‘a’, 5) = ‘f’ and shift(‘x’, 0) = ‘x’. For every odd index i, you want to replace the digit s[i] with shift(s[i-1], s[i])....

2022-11-24 November Thursday · 1 分鐘 · Tony

Leetcode1662 Check If Two String Arrays are Equivalent

題目 Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise. A string is represented by an array if the array elements concatenated in order forms the string. Example Input: word1 = ["ab", "c"], word2 = ["a", "bc"] Output: true Explanation: word1 represents string "ab" + "c" -> "abc" word2 represents string "a" + "bc" -> "abc" The strings are the same, so return true....

2022-11-24 November Thursday · 1 分鐘 · Tony

Leetcode1832 Check if the Sentence Is Pangram

題目 A pangram is a sentence where every letter of the English alphabet appears at least once. Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise. Example Input: sentence = "thequickbrownfoxjumpsoverthelazydog" Output: true Explanation: sentence contains at least one of every letter of the English alphabet. 題意: sentence中至少要出現一次a-z 解法 import ( "strings" ) func checkIfPangram(sentence string) bool { if len(sentence) < 26 { return false } for i := 'a'; i <= 'z'; i++ { if strings....

2022-11-24 November Thursday · 1 分鐘 · Tony

Leetcode2325 Decode the Message

題目 You are given the strings key and message, which represent a cipher key and a secret message, respectively. The steps to decode message are as follows: Use the first appearance of all 26 lowercase English letters in key as the order of the substitution table. Align the substitution table with the regular English alphabet. Each letter in message is then substituted using the table. Spaces ’ ’ are transformed to themselves....

2022-11-24 November Thursday · 2 分鐘 · Tony

Leetcode1528 Shuffle String

題目 You are given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string. Return the shuffled string. Example 1 Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] Output: "leetcode" Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. 題意: indices的值為字串s替換要替換的位置 字串 : 位置 c -> 4 o -> 5 d -> 6 依此類推 解法 func restoreString(s string, indices []int) string { res := make([]byte, len(s)) for k, v := range indices { res[v] = s[k] } return string(res) } 加個測試,看看結果是不是如預期 import ( "github....

2022-11-13 November Sunday · 1 分鐘 · Tony

Leetcode2011 Final Value of Variable After Performing Operations

題目 There is a programming language with only four operations and one variable X: ++X and X++ increments the value of the variable X by 1. –X and X– decrements the value of the variable X by 1. Initially, the value of X is 0. Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations. Example Input: operations = ["--X","X++","X++"] Output: 1 Explanation: The operations are performed as follows: Initially, X = 0....

2022-11-06 November Sunday · 1 分鐘 · Tony

Leetcode1108 Defanging an IP Address

題目 Given a valid (IPv4) IP address, return a defanged version of that IP address. A defanged IP address replaces every period “.” with “[.]”. ## Example 1: Input: address = "1.1.1.1" Output: "1[.]1[.]1[.]1" Example 2: Input: address = "255.100.50.0" Output: "255[.]100[.]50[.]0" 題意: 將字串.替換成[.]就好了 解法 import ( "strings" ) func defangIPaddr(address string) string { return strings.Replace(address, ".", "[.]", -1) } 加個測試,看看結果是不是如預期 import ( "github.com/stretchr/testify/assert" "testing" ) func Test_defangIPaddr(t *testing.T) { address := "1....

2022-11-06 November Sunday · 1 分鐘 · Tony

Redis Exporter建立

前言 redis exporter是用來監控Redis一個Prometheus Client,提供相關redis的監控metrics 步驟 建立一個新的folder,命名redis 在redis目錄底下,建立docker-compose.yml docker-compose.yml 底下輸入 version: '3' services: redis: restart: always container_name: redis image: redis:6.2.7-alpine ports: - 6379:6379 redis-exporter: restart: always image: oliver006/redis_exporter:v1.45.0-alpine container_name: redis-exporter ports: - 9121:9121 command: - '--redis.addr=redis://redis-server:6379' 執行docker-compose up -d,redis服務與redis exporter即可啟動 確認redis exporter服務,已經在執行輸入curl http://localhost:9121/metrics λ curl http://localhost:9121/metrics # HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles. # TYPE go_gc_duration_seconds summary go_gc_duration_seconds{quantile="0"} 0 go_gc_duration_seconds{quantile="0.25"} 0 go_gc_duration_seconds{quantile="0.5"} 0 go_gc_duration_seconds{quantile="0.75"} 0 go_gc_duration_seconds{quantile="1"} 0 go_gc_duration_seconds_sum 0 go_gc_duration_seconds_count 0 # HELP go_goroutines Number of goroutines that currently exist....

2022-10-31 October Monday · 1 分鐘 · Tony