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)
  • 相关阅读:
    基于perl的网络爬虫
    ios cell展示可滑动的图片
    iOS计算字符串的宽度高度
    swift水波效果
    iOS添加另一个控制器的时候要注意啊
    swift隐藏显示导航栏的底线
    swift集成alamofire的简单封装
    tableview详细介绍
    xmpp xml基本语义
    xmpp SASL 定义
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12786475.html
Copyright © 2011-2022 走看看