每日一Leet

34 回覆
2 Like 3 Dislike
2022-05-31 23:43:25
Expressive Words
Sometimes people repeat letters to represent extra feeling. For example:

- "hello" -> "heeellooo"
- "hi" -> "hiiii"
In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo".

You are given a string s and an array of query strings words. A query word is stretchy if it can be made to be equal to s by any number of applications of the following extension operation: choose a group consisting of characters c, and add some number of characters c to the group so that the size of the group is three or more.

- For example, starting with "hello", we could do an extension on the group "o" to get "hellooo", but we cannot get "helloo" since the group "oo" has a size less than three. Also, we could do another extension like "ll" -> "lllll" to get "helllllooo". If s = "helllllooo", then the query word "hello" would be stretchy because of these two extension operations: query = "hello" -> "hellooo" -> "helllllooo" = s.
Return the number of query strings that are stretchy.

Example 1:

Input: s = "heeellooo", words = ["hello", "hi", "helo"]
Output: 1
Explanation: 
We can extend "e" and "o" in the word "hello" to get "heeellooo".
We can't extend "helo" to get "heeellooo" because the group "ll" is not size 3 or more.


Example 2:

Input: s = "zzzzzyyyyy", words = ["zzyy","zy","zyy"]
Output: 3

Constraints:

- 1 <= s.length, words.length <= 100
- 1 <= words[i].length <= 100
- s and words[i] consist of lowercase letters. 


/**
 * @param {string} s
 * @param {string[]} words
 * @return {number}
 */
func expressiveWords(s string, words []string) int {
 var convertStrToCharMap = func(str string) map[string][]int {
  lastChar := ""
  charMap := make(map[string][]int)

  for _, char := range strings.Split(str, "") {
   if char != lastChar {
    charMap[char] = append(charMap[char], 1)
   } else {
    charMap[char][len(charMap[char])-1]++
   }
   lastChar = char
  }
  return charMap
 }

 inputCharMap := convertStrToCharMap(s)

 checkWord := func(charMap map[string][]int, word string) bool {
  wordCharMap := convertStrToCharMap(word)
  for key := range charMap {
   if len(charMap[key]) != len(wordCharMap[key]) {
    return false
   }

   for i := 0; i < len(charMap[key]); i++ {
    if charMap[key][i] < wordCharMap[key][i] ||
     (charMap[key][i] < 3 && charMap[key][i] != wordCharMap[key][i]) ||
     wordCharMap[key][i] == 0 {
     return false
    }
   }
  }
  return true
 }

 output := 0
 for _, word := range words {
  var wg sync.WaitGroup
  wg.Add(1)
  go func(w string) {
   defer wg.Done()
   if checkWord(inputCharMap, w) {
    output++
   }
  }(word)
  wg.Wait()
 }
 return output
}

呢題唔識做 寫得好差
2022-06-01 19:52:46
Running Sum of 1d Array

Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

Return the running sum of nums.

Example 1:

Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].

Example 2:

Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].

Example 3:
Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]


Constraints:
1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6


Answer:
/**
 * @param {number[]} nums
 * @return {number[]}
 */
const runningSum = (nums)=>{
  const numsLength = nums.length
  return nums.reduce((result, curr, index)=>{
     result.push(curr+result[index])
     return result
  }, [0]).slice(1)  
};


JS reduce 很好用
2022-06-01 19:56:07
/**
 * @param {number[]} nums
 * @return {number[]}
 */
const runningSum = (nums)=>{
  return nums.reduce((result, curr, index)=>{
     result.push(curr+result[index])
     return result
  }, [0]).slice(1)  
};

有行del少咗
2022-06-02 20:22:16
Reduce個initial value 呀嘛
用咗result第一次行會係[0]開始
如果唔係[0]第一次行result會係由input第一個value開始
input係[int,int...] 所以result會係一個int 而int[index]會係undefine
我又唔想reduce行緊嗰時每次check result[index]係唔係undefine 所以俾個0佢之後slice返
const runningSum = (nums)=>{
  let result = []
  nums.reduce((prev, curr)=>{
     result.push(prev+curr)
  }, 0)
  return result
};

其實現實咁寫會好啲 我貪快
不過最後睇人地做用map原來一行做到 仲簡單
2022-06-02 20:24:45
const runningSum = (nums)=>{
  let result = []
  nums.reduce((prev, curr)=>{
     const sum=prev+curr
     result.push(sum)
     return sum
  }, 0)
  return result
};

咁先啱
2022-06-02 20:35:43
forloop快好多 啱啱試咗呢題快20%左右
2022-06-02 20:44:52
Transpose Matrix
Given a 2D integer array matrix, return the transpose of matrix.

The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.

Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,4,7],[2,5,8],[3,6,9]]


Example 2:
Input: matrix = [[1,2,3],[4,5,6]]
Output: [[1,4],[2,5],[3,6]]


Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 1000
1 <= m * n <= 105
-109 <= matrix[i][j] <= 109


Answer:
func transpose(matrix [][]int) [][]int {
 transposeOfMatrix := make([][]int, len(matrix[0]))
 for i := 0; i < len(matrix[0]); i++ {
  for j := 0; j < len(matrix); j++ {
            transposeOfMatrix[i] = append(transposeOfMatrix[i], matrix[j][i])
  }
 }
 return transposeOfMatrix
}

順手做埋
吹水台自選台熱 門最 新手機台時事台政事台World體育台娛樂台動漫台Apps台遊戲台影視台講故台健康台感情台家庭台潮流台美容台上班台財經台房屋台飲食台旅遊台學術台校園台汽車台音樂台創意台硬件台電器台攝影台玩具台寵物台軟件台活動台電訊台直播台站務台黑 洞