zoukankan      html  css  js  c++  java
  • Leetcode练习(Python):递归类:面试题16. 数值的整数次方:实现函数double Power(double base, int exponent),求base的exponent次方。不得使用库函数,同时不需要考虑大数问题。

    题目:
    数值的整数次方:实现函数double Power(double base, int exponent),求base的exponent次方。不得使用库函数,同时不需要考虑大数问题。
    思路:
    递归,二分法
    程序:
    class Solution:
        def myPow(self, x: float, n: int) -> float:
            if x == 0:
                return 0
            if n == 0:
                return 1
            if n == 1:
                return x
            if n >= 0:
                if n % 2 == 0:
                    return self.myPow(x * x, n // 2)
                else:
                    return self.myPow(x * x, n // 2) * x
            if n < 0:
                if (-n) % 2 == 0:
                    return self.myPow(1 / (x * x), (-n) // 2)
                else:
                    return self.myPow(1 / (x * x), (-n) // 2) * (1 / x)
  • 相关阅读:
    C#使用GZipStream压缩与解压字符串
    C# 参考之方法参数关键字:params、ref及out
    C#中cookie讲解
    WPF中窗口控件的跨线程调用
    vs2010 快捷键大全
    Python线程池
    Python 爬虫修正
    Python 爬虫插件
    Python发现爬虫插件有BUG。
    Python 存在BUG的线程池
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12786475.html
Copyright © 2011-2022 走看看