返回介绍

solution / 2400-2499 / 2469.Convert the Temperature / README_EN

发布于 2024-06-17 01:03:05 字数 3302 浏览 0 评论 0 收藏 0

2469. Convert the Temperature

中文文档

Description

You are given a non-negative floating point number rounded to two decimal places celsius, that denotes the temperature in Celsius.

You should convert Celsius into Kelvin and Fahrenheit and return it as an array ans = [kelvin, fahrenheit].

Return _the array ans. _Answers within 10-5 of the actual answer will be accepted.

Note that:

  • Kelvin = Celsius + 273.15
  • Fahrenheit = Celsius * 1.80 + 32.00

 

Example 1:

Input: celsius = 36.50
Output: [309.65000,97.70000]
Explanation: Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70.

Example 2:

Input: celsius = 122.11
Output: [395.26000,251.79800]
Explanation: Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.

 

Constraints:

  • 0 <= celsius <= 1000

Solutions

Solution 1: Simulation

We can directly simulate according to the problem description.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

class Solution:
  def convertTemperature(self, celsius: float) -> List[float]:
    return [celsius + 273.15, celsius * 1.8 + 32]
class Solution {
  public double[] convertTemperature(double celsius) {
    return new double[] {celsius + 273.15, celsius * 1.8 + 32};
  }
}
class Solution {
public:
  vector<double> convertTemperature(double celsius) {
    return {celsius + 273.15, celsius * 1.8 + 32};
  }
};
func convertTemperature(celsius float64) []float64 {
  return []float64{celsius + 273.15, celsius*1.8 + 32}
}
function convertTemperature(celsius: number): number[] {
  return [celsius + 273.15, celsius * 1.8 + 32];
}
impl Solution {
  pub fn convert_temperature(celsius: f64) -> Vec<f64> {
    vec![celsius + 273.15, celsius * 1.8 + 32.0]
  }
}
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
double* convertTemperature(double celsius, int* returnSize) {
  double* ans = malloc(sizeof(double) * 2);
  ans[0] = celsius + 273.15;
  ans[1] = celsius * 1.8 + 32;
  *returnSize = 2;
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文