实现 pow(x, n)。
示例 1:
输入: 2.00000, 10
输出: 1024.00000
示例 2:
输入: 2.10000, 3
输出: 9.26100
详见:https://leetcode.com/problems/powx-n/description/

Java实现:

方法一:

class Solution {
public double myPow(double x, int n) {
if(n<0){
return 1/helper(x,-n);
}
return helper(x,n);
}
private double helper(double x,int n){
if(n==0){
return 1;
}
double half=helper(x,n/2);
if(n%2==0){
return half*half;
}
return x*half*half;
}
}

方法二:

class Solution {
public double myPow(double x, int n) {
double res=1.0;
for(int i=n;i!=0;i/=2){
if(i%2!=0){
res*=x;
}
x*=x;
}
return n<0?1/res:res;
}
}

参考:https://www.cnblogs.com/grandyang/p/4383775.html

050 Pow(x, n)的更多相关文章

  1. Java for LeetCode 050 Pow(x, n)

    Implement pow(x, n). 解题思路: 直接使用乘法实现即可,注意下,如果n很大的话,递归次数会太多,因此在n=10和n=-10的地方设置一个检查点,JAVA实现如下: static p ...

  2. 力扣算法题—050计算pow(x, n)

    #include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...

  3. 【探索】无形验证码 —— PoW 算力验证

    先来思考一个问题:如何写一个能消耗对方时间的程序? 消耗时间还不简单,休眠一下就可以了: Sleep(1000) 这确实消耗了时间,但并没有消耗 CPU.如果对方开了变速齿轮,这瞬间就能完成. 不过要 ...

  4. [LeetCode] Super Pow 超级次方

    Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...

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

    Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...

  6. Javascript四舍五入(Math.round()与Math.pow())

    代码 Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ ...

  7. Pow(x, n)

    Implement pow(x, n). public class Solution { public double pow(double x, int n) { //判断x是不是0 if(Math. ...

  8. leetcode pow(x,n)实现

    题目描述: 自己实现pow(double x, int n)方法 实现思路: 考虑位运算.考虑n的二进制表示形式,以n=51(110011)为例,x^51 = x^1*x^2*x^16*x^32,因此 ...

  9. C语言pow函数编写

    C语言pow函数编写 #include<stdio.h> double chaoba(double f,double q); //声明自定义函数 void main(void) { dou ...

随机推荐

  1. Linux_服务器_03_xxx is not in the sudoers file.This incident will be reported.的解决方法

    1.切换到root用户下,怎么切换就不用说了吧,不会的自己百度去. 2.添加sudo文件的写权限,命令是:chmod u+w /etc/sudoers 3.编辑sudoers文件vi /etc/sud ...

  2. linux 进程学习笔记-暂停进程

    <!--[if !supportLists]-->Ÿ <!--[endif]-->暂停进程 int pause() 其会挂起当前进程直到有信号来唤醒或者进程被结束. 随便提一下 ...

  3. MySQL多个条件以什么表当做主条件表_20161111周五

    前两天有事情 停了2天 数据需求:1.活动日期11.8-11.10订单2.单笔订单购买A类产品 B类产品满足68元.且连续3天下单的用户ID 首先第一个条件很简单,主要是第二个条件 第二个条件是 且 ...

  4. bzoj 2216: Lightning Conductor 单调队列优化dp

    题目大意 已知一个长度为\(n\)的序列\(a_1,a_2,...,a_n\)对于每个\(1\leq i\leq n\),找到最小的非负整数\(p\)满足: 对于任意的\(j\), \(a_j \le ...

  5. bzoj4555: 求和sum 快速傅立叶变换

    题目大意 给定\(S(n,m)\)表示第二类斯特林数,定义函数\(f(n)\) \[f(n) = \sum_{i=0}^n\sum_{j=0}^iS(i,j)*2^j*(j!)\] 给定正整数\(n, ...

  6. PowerShell自动部署网站—(2)、安装.Net Framework

    #$PSScriptRoot = "D:\Website":$PSScriptRoot 用于获取执行脚本所在的目录,但是PowerShell 2.0 不支持,需要人为赋值成绝对路径 ...

  7. XCode工程中 Project 和 Targets区别

    转自:http://blog.csdn.net/zhaozy55555/article/details/8557175 project就是一个项目,或者说工程,一个project可以对应多个targe ...

  8. 用python监控您的window服务

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://world77.blog.51cto.com/414605/782935 最近比较 ...

  9. huipengly的2018年度总结

    一.技术 1.入门C++ 今年看完了一本很厚很厚的书——<c++ primer 5th>.从头到尾,基本上每一个课后练习题都完成了.入门了C++这个大坑,也初步了解了面向对象这个程序抽象方 ...

  10. 25.ProfileService实现(调试)

    上一节课拿到的AccessToken和IdToken 实现ProfileService类 在服务端 添加ProfileService类 需要继承IProfileServiuce 用到的画图工具 Ipr ...