LeetCode 题解工作台
温度转换
给你一个四舍五入到两位小数的非负浮点数 celsius 来表示温度,以 摄氏度 ( Celsius )为单位。 你需要将摄氏度转换为 开氏度 ( Kelvin )和 华氏度 ( Fahrenheit ),并以数组 ans = [kelvin, fahrenheit] 的形式返回结果。 返回数组 an…
1
题型
7
代码语言
3
相关题
当前训练重点
简单 · 数学·driven
答案摘要
直接根据题意模拟即可。 时间复杂度 ,空间复杂度 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数学·driven 题型思路
题目描述
给你一个四舍五入到两位小数的非负浮点数 celsius 来表示温度,以 摄氏度(Celsius)为单位。
你需要将摄氏度转换为 开氏度(Kelvin)和 华氏度(Fahrenheit),并以数组 ans = [kelvin, fahrenheit] 的形式返回结果。
返回数组 ans 。与实际答案误差不超过 10-5 的会视为正确答案。
注意:
开氏度 = 摄氏度 + 273.15华氏度 = 摄氏度 * 1.80 + 32.00
示例 1 :
输入:celsius = 36.50 输出:[309.65000,97.70000] 解释:36.50 摄氏度:转换为开氏度是 309.65 ,转换为华氏度是 97.70 。
示例 2 :
输入:celsius = 122.11 输出:[395.26000,251.79800] 解释:122.11 摄氏度:转换为开氏度是 395.26 ,转换为华氏度是 251.798 。
提示:
0 <= celsius <= 1000
解题思路
方法一:模拟
直接根据题意模拟即可。
时间复杂度 ,空间复杂度 。
class Solution:
def convertTemperature(self, celsius: float) -> List[float]:
return [celsius + 273.15, celsius * 1.8 + 32]
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Candidate should demonstrate understanding of mathematical conversions.
- question_mark
Look for clear implementation of formulas without unnecessary complexity.
- question_mark
Ensure the candidate handles precision correctly when returning the results.
常见陷阱
外企场景- error
Not applying the correct formulas for Kelvin and Fahrenheit conversion.
- error
Failing to round the output to 5 decimal places as specified in the problem statement.
- error
Incorrectly handling edge cases, like 0 Celsius or extreme values close to the upper constraint.
进阶变体
外企场景- arrow_right_alt
Extend the problem to accept negative Celsius values.
- arrow_right_alt
Add a conversion for additional temperature scales, like Rankine or Delisle.
- arrow_right_alt
Introduce a requirement for an array of Celsius values and return a corresponding array of Kelvin and Fahrenheit values.