LeetCode50. Pow(x, n)

文章目录

一、题目

Implement pow(x, n), which calculates x raised to the power n (i.e., xn).

Example 1:

Input: x = 2.00000, n = 10
Output: 1024.00000
Example 2:

Input: x = 2.10000, n = 3
Output: 9.26100
Example 3:

Input: x = 2.00000, n = -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25

Constraints:

-100.0 < x < 100.0
-231 <= n <= 231-1
n is an integer.
Either x is not zero or n > 0.
-104 <= xn <= 104

二、题解

class Solution {
   
public:
    double quickMul(double x,long long N){
   
        double res = 1.0;
        while(N > 0){
   
            if((N & 1) == 1){
   
                res *= x;
            }
            x *= x;
            N = N >> 1;
        }
        return res;
    }
    double myPow(double x, int n) {
   
        long long N = n;
        return N > 0 ? quickMul(x,N) : 1.0 / quickMul(x,-N);
    }
};

相关推荐

  1. LeetCode50. Pow(x, n)

    2024-01-05 17:04:06       52 阅读
  2. LeetCode--55

    2024-01-05 17:04:06       54 阅读
  3. LeetCode--58

    2024-01-05 17:04:06       43 阅读
  4. LeetCode 每日一题 Day 47 - 50

    2024-01-05 17:04:06       54 阅读
  5. LeetCode 每日一题 Day 51 - 53

    2024-01-05 17:04:06       51 阅读
  6. 商城数据库(51 52 53 54 55 56 57 58 59 60)

    2024-01-05 17:04:06       30 阅读
  7. LeetCode56. Merge Intervals

    2024-01-05 17:04:06       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-05 17:04:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-05 17:04:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-05 17:04:06       82 阅读
  4. Python语言-面向对象

    2024-01-05 17:04:06       91 阅读

热门阅读

  1. 小柱的笔记

    2024-01-05 17:04:06       44 阅读
  2. AI+金融:大模型引爆金融科技革命

    2024-01-05 17:04:06       50 阅读
  3. LLM之Agent再探

    2024-01-05 17:04:06       64 阅读
  4. Flink学习-处理函数

    2024-01-05 17:04:06       57 阅读
  5. argparse学习使用

    2024-01-05 17:04:06       66 阅读
  6. Elasticsearch 优化

    2024-01-05 17:04:06       50 阅读