50. Pow(x, n)

题目描述

实现 pow(x, n),即计算 x 的 n 次幂函数。

每日一算法2019/5/15Day 12LeetCode50. Pow(x, n)

示例 1:

输入: 2.00000, 10
输出: 1024.00000

示例 2:

输入: 2.10000, 3
输出: 9.26100

示例 3:

输入: 2.00000, -2
输出: 0.25000
解释: 2-2 = 1/22 = 1/4 = 0.25

说明:

  • -100.0 < x < 100.0
  • n 是 32 位有符号整数,其数值范围是 [−231, 231 − 1]。

Java 实现

class Solution {
public double myPow(double x, int n) {
if (x == 1 || n == 0) {
return 1;
}
if (n == Integer.MIN_VALUE) {
if (x == -1) {
return 1;
} else {
return 0;
}
}
if (n < 0) {
n *= -1;
x = 1 / x;
}
return (n % 2 == 0) ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2);
}
}

相似题目

参考资料

LeetCode 50. Pow(x, n) 12的更多相关文章

  1. LeetCode - 50. Pow(x, n)

    50. Pow(x, n) Problem's Link ----------------------------------------------------------------------- ...

  2. leetcode 50. Pow(x, n) 、372. Super Pow

    50. Pow(x, n) 372. Super Pow https://www.cnblogs.com/grandyang/p/5651982.html https://www.jianshu.co ...

  3. Java实现 LeetCode 50 Pow(x,n)

    50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...

  4. [LeetCode] 50. Pow(x, n) 求x的n次方

    Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...

  5. LeetCode 50 Pow(x, n) (实现幂运算)

    题目链接:https://leetcode.com/problems/powx-n/?tab=Description   Problem:实现幂运算即 pow(x,n)   设形式为pow(x,n)  ...

  6. LeetCode 50 - Pow(x, n) - [快速幂]

    实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10输出: 1024.00000 示例 2: 输入: 2.10000, 3输出: 9.26100 示例 ...

  7. [leetcode]50. Pow(x, n)求幂

    Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Ou ...

  8. Leetcode——50.Pow(x, n)

    @author: ZZQ @software: PyCharm @file: leetcode50_myPow.py @time: 2018/11/22 13:58 要求:实现 pow(x, n) , ...

  9. Leetcode 50.Pow(x,n) By Python

    实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, 3 输出: 9.26100 ...

随机推荐

  1. App弱网测试与常用模拟工具

    iOS平台,通过自带的开发者选项 >Network Link Conditioner, 即可简单的模拟各种速度的网络情况: 通过抓包工具,设置延迟,进行模拟不同的网络情况,比如常用的fiddle ...

  2. html转图片/html2canvas的使用/星座测试/类似于损友圈的活动

    https://try.fishqc.com/Activity/constellation ---成品 电脑上录的gif 有借鉴的链接,很多,下面这个还不错~先别看,尊重下我先~~~~ https:/ ...

  3. csapp网络编程初学笔记

    csapp网络编程初学笔记 客户端-服务器编程模型 每个网络应用都是基于客户端-服务器模型,服务器管理某种资源,并且通过操作来为它的客户提供某种服务 客户端-服务器模型中的基本操作是transacti ...

  4. Wrapper: Error - Unable to execute Java command

    在64位的系统下 将短信程序运行于服务中,出现以下错误: Error: [size=14px; line-height: 26px;]FATAL  | wrapper  | 2012/06/18 17 ...

  5. [String]两个右补空格使字符串达到固定长度的函数 来自网上 请君自取

    代码: package fixsizestring; public class TestClass { public static void main(String[] args) { for(int ...

  6. 扩展和嵌入 Python 解释器 用 C 或 C++ 编写模块以使用新模块来扩展 Python 解释器的功能 定义新的函数\对象类型\方法。 将 Python 解释器嵌入到另一个应用程序中

    // https://python3-cookbook.readthedocs.io/zh_CN/latest/c15/p02_write_simple_c_extension_module.html ...

  7. SonarQube Detection of Injection Flaws in Java, C#, PHP

    Code Quality Tools Review: Sonar, PMD, Findbugs and Checkstyle Sonar CheckStyle, FindBugs and PMD - ...

  8. VS Code 通过文件名查询文件并打开

    On Windows press Ctrl+p or Ctrl+e On Mac press Cmd+p on the Linux press also Ctrl+p works Older Mac ...

  9. zookeeper使用过程的错误

    一.zookeeper启动成功,dubbo服务也注册成功,但是服务消费者调用失败 报错如下: [myid:] - INFO [SyncThread:0:ZooKeeperServer@645] - E ...

  10. Navigator的使用:

    1.路由直接跳转到下一个页面: Navigator.pushNamed(context,"/login"); 2.跳转的下一个页面,替换当前的页面: Navigator.of(co ...