↓快轉到主要內容
  1. LeetCode/

LeetCode 3838: Weighted Word Mapping

·1 分鐘· ·
LeetCode Easy Array String Simulation
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: easy 第一次嘗試:2026-02-14

  • 提交紀錄:Accepted(一次通過)

解題思路
#

每個字算出權重總和 w,取 r = w % 26。反向字母順序是 0 -> 'z'、1 -> 'y'…,所以對應的字母是 chr(ord('z') - r)(Array 模板五)。

我的解法
#

class Solution:
    def mapWordWeights(self, words: List[str], weights: List[int]) -> str:
        return "".join(
            chr(ord("z") - sum(weights[ord(c) - ord("a")] for c in w) % 26)
            for w in words
        )

複雜度
#

時間 O(總字元數),空間 O(字數)。

要特別避免的錯誤
#

  • 是反向對應,0 對到 z,不是 a。

面試口說整理
#

我對每個字加總字母的權重再 mod 26,用 ord('z') 減掉餘數得到反向對應的字母,最後串起來。時間 O(總字元數)。

相關文章

LeetCode 3379: Transformed Array
·1 分鐘
LeetCode Easy Array Simulation
LeetCode 3379 解題筆記:往右或往左走 nums[i] 步都可以寫成 nums[(i + nums[i]) % n];Python 的 % 對負數也回傳非負值
LeetCode 3813: Vowel-Consonant Score
·1 分鐘
LeetCode Easy String Simulation
LeetCode 3813 解題筆記:只數字母,分成母音和子音;子音是 0 時分數是 0,否則用整數除法
LeetCode 3701: Compute Alternating Sum
·1 分鐘
LeetCode Easy Array Simulation
LeetCode 3701 解題筆記:偶數 index 加、奇數 index 減;用切片就是 sum(nums[::2]) - sum(nums[1::2])
LeetCode 3688: Bitwise OR of Even Numbers in an Array
·1 分鐘
LeetCode Easy Bit-Manipulation Array Simulation
LeetCode 3688 解題筆記:只對偶數做 OR;初始值 0 剛好也是「沒有偶數」時的答案
LeetCode 1920: Build Array from Permutation
·1 分鐘
LeetCode Easy Array Simulation
LeetCode 1920 解題筆記:照定義 ans[i] = nums[nums[i]];要 O(1) 額外空間時,把新值乘 n 疊在原本的值上
LeetCode 3069: Distribute Elements Into Two Arrays I
·1 分鐘
LeetCode Easy Array Two-Pointers Simulation
LeetCode 3069 解題筆記:照規則模擬:前兩個各放一邊,之後比較兩個陣列的最後一個,放到最後一個比較大的那邊