实现 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. spring属性注入DI

    spring setter方式注入: 注入对象属性: 前提: 在bean对应实体中有对应的setter方法. 基础代码: 在bean中有另一个bean属性的setter方法. package cn.i ...

  2. zepto.fullpage

    内容来自:颜海镜 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  3. CodeForces - 311B:Cats Transport (DP+斜率优化)

    Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight r ...

  4. bzoj 3714 [PA2014]Kuglarz——思路+最小生成树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3714 如果用s[ i ]表示前 i 个的奇偶性,那么c(i_j)表示s[ i-1 ]^s[ ...

  5. CentOS 6.6 搭建Zabbix 3.0.3 过程

    分享CentOS 6.6下搭建Zabbix 3.0.3 的过程,希望都大家有所帮助. 环境安装 系统环境: # cat /etc/RedHat-release CentOS release 6.6 ( ...

  6. Arduino 视频教程

    http://v.youku.com/v_show/id_XNDU1MjI4MzA4.html?from=y1.2-1-176.3.12-2.1-1-1-11

  7. There&nbsp;is&nbsp;no&nbsp;resul…

    There is no result type defined for type 'json' mapped with name 'success'. 这个错误是json初学者很容易遇到的错误:现在把 ...

  8. 下拉选择select和复选框checkbox的状态的各种方式

    复选框的状态 <input name="ck" value=" " type="checkbox"  checked> 或者&l ...

  9. MS SQL的CASE...WHEN...THEN...END语法

    根据多个可能的答案检查一个值或变量. 举例说明: SELECT [type],CASE [type] WHEN 'TT' THEN 'TYPE_TABLE' WHEN 'FN' THEN 'SQL_S ...

  10. js 读本地文件

    http://www.jb51.net/article/21191.htm <!doctype html> <html lang="en"> <hea ...