Implement pow(x, n), which calculates x raised to the power n (xn).

Example 1:

Input: 2.00000, 10
Output: 1024.00000
Example 2: Input: 2.10000, 3
Output: 9.26100
Example 3: Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
Note: -100.0 < x < 100.0
n is a 32-bit signed integer, within the range [−231, 231 − 1]

Solution: recusrion (think about better recursion(memo))

class Solution {
//when use Math.abs(Integer.MIN_VALUE) overflow
public double myPow(double x, int n) {
//make subset (recursive)
if(n > 0) return powHelper(x, n);
else if(n<0) return 1/powHelper(x,n);//no need -n
else return 1.0;
}
double powHelper(double x, int n){
if(n==0) return 1.0;
double y = powHelper(x, n/2);
if(n%2==0) return y*y;
else return y*y*x;
}
}

Solution

class Solution {
//n is even(y*y) and n is odd(y*y*x) public double myPow(double x, int n) {
long len = Math.abs((long) n);//point 1: long type
double res = 1;
while(len!=0){
if(len%2!=0) res = res*x;
x = x*x;//why
len/=2;
}
if(n < 0) return 1/res;
else return res;
}
}

Memo + crack the interview

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

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

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

  2. [Leetcode][Python]50: Pow(x, n)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 50: Pow(x, n)https://leetcode.com/probl ...

  3. 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 ...

  4. LeetCode 50. Pow(x, n) 12

    50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...

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

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

  6. 刷题-力扣-50. Pow(x, n)

    50. Pow(x, n) 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/powx-n/ 著作权归领扣网络所有.商业转载请联系官方授 ...

  7. [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 ...

  8. 50. Pow(x, n) (编程技巧)

    Implement pow(x, n). double sum = 1; if (n > 0) { while ((n--) > 0) sum *= x; return sum; } el ...

  9. 50. Pow(x, n)

    题目: Implement pow(x, n). 链接: http://leetcode.com/problems/powx-n/ 题解: 使用二分法求实数幂,假如不建立临时变量halfPow,直接r ...

随机推荐

  1. 读写锁--ReentrantReadWriteLock

    读写锁,对于读操作来说是共享锁,对于写操作来说是排他锁,两种操作都可重入的一种锁.底层也是用AQS来实现的,我们来看一下它的结构跟代码: ------------------------------- ...

  2. unity代码设置鼠标样式

    public class Main : MonoBehaviour { public Texture2D cursorTexture;//图片 public CursorMode cursorMode ...

  3. mysql启动登陆

    mysql.server start    # 1. 启动 mysql.server stop     # 2. 停止 mysql.server restart  # 3. 重启 1.本地登陆 sud ...

  4. [转] 多种方法查看Oracle SQL执行计划

    本文转自:http://falchion.iteye.com/blog/616234 一.在线查看执行计划表 如果PLAN_TABLE表不存在,执行$ORACLE_HOME/rdbms/admin/u ...

  5. c#实现16进制和字符串之间转换的代码

    以下示例演示如何执行下列任务: 获取字符串中每个字符的十六进制值. 获取与十六进制字符串中的每个值对应的字符. 将十六进制 string 转换为整型. 将十六进制 string 转换为浮点型. 将字节 ...

  6. C++(笔)002

    #include <iostream> //预处理器编译指令 int main() //函数头:对函数和程序其它部份之间的接口作出总结 int:函数的返回类型 { using namesp ...

  7. python模块之contexlib

    一.上下文管理器 with是python实现上下文管理器的核心关键词.它能够在代码执行前和执行后做一些额外的事情. 最常见的代码恐怕就是文件操作了. with open("file" ...

  8. 6、springboot之根目录设置

    访问的时候用

  9. linux chkconfig 使用说明

    原文 chkconfig是管理系统服务(service)的命令行工具.所谓系统服务(service),就是随系统启动而启动,随系统关闭而关闭的程序. chkconfig可以更新(启动或停止)和查询系统 ...

  10. centos安装后,连接不上网络,yum命令执行can not find a valid baseurl for repo: base/7/x86-64

    检查了网络适配器是NAT模式没问题,按照网友的方法成功解决: 1.vi /etc/sysconfig/network-scripts/ifcfg-ens123(不是每个主机都是ens123)  把ON ...