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

LeetCode 2469: Convert the Temperature

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

基本資料
#

難易度: easy 第一次嘗試:2025-12-17

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

解題思路
#

題目給了兩個轉換公式,直接套用,依序回傳 [Kelvin, Fahrenheit]。允許 1e-5 的誤差,所以浮點數運算沒問題。

我的解法
#

我當時用 C++ 寫,這裡放等價的 Python:

class Solution:
    def convertTemperature(self, celsius: float) -> List[float]:
        return [celsius + 273.15, celsius * 1.8 + 32]

複雜度
#

時間 O(1),空間 O(1)。

要特別避免的錯誤
#

  • 回傳順序是 Kelvin 在前、Fahrenheit 在後。

面試口說整理
#

直接套兩個公式回傳。題目允許 1e-5 的誤差,所以浮點數誤差不影響。時間 O(1)。

相關文章

LeetCode 3697: Compute Decimal Representation
·1 分鐘
LeetCode Easy Math Array
LeetCode 3697 解題筆記:每個非 0 的位數乘上它的位值就是一個分量,從高位到低位輸出
LeetCode 3550: Smallest Index With Digit Sum Equal to Index
·1 分鐘
LeetCode Easy Math Array
LeetCode 3550 解題筆記:由小到大檢查每個 index,算位數和,第一個相等的就是答案
LeetCode 1979: Find Greatest Common Divisor of Array
·1 分鐘
LeetCode Easy Math Number-Theory Gcd Array
LeetCode 1979 解題筆記:找出最小值和最大值,用輾轉相除法算 GCD
LeetCode 2235: Add Two Integers
·1 分鐘
LeetCode Easy Math
LeetCode 2235 解題筆記:回傳兩數相加;C++ 版本忘了分號編譯失敗一次
LeetCode 2413: Smallest Even Multiple
·1 分鐘
LeetCode Easy Math Number-Theory
LeetCode 2413 解題筆記:lcm(2, n);n 是偶數就是 n,奇數就是 2n
LeetCode 1304: Find N Unique Integers Sum up to Zero
·1 分鐘
LeetCode Easy Math Array
LeetCode 1304 解題筆記:放 1..n/2 和它們的相反數,兩兩抵銷;n 是奇數時再加一個 0