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. 【wordpress】wordpress环境的搭建

    WordPress WordPress 是一种使用 PHP语言和 MySQL数据库开发的开源.免费的Blog(博客,网志)引擎,用户可以在支持 PHP 和 MySQL 数据库的服务器上建立自己的 Bl ...

  2. SQL Cookbook—数字、日期

    1.计算不包含最大值和最小值的均值2.把字母数字串转换为数值3.更改累计和中的值–显示存款或取款后的值4.加减日.月.年5.计算两个日期之间的天数6.确定两个日期之间的工作日数目表EMP中,计算BLA ...

  3. step3: 创建jobbole爬虫

    scrapy startproject Redbacktestcd Redbacktest 创建jobbole爬虫 scrapy genspider jobbole2 blog.jobbole.com ...

  4. BDD框架之Cucumber研究

    BDD框架之Cucumber研究 引用链接:http://kongqingyun123.blog.163.com/blog/static/6377283520134158437813/ Cucumbe ...

  5. 使用Myeclipse进行简单webservice开发的示例

    (转) http://blog.csdn.net/changhenshui1990/article/details/70142371 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要 ...

  6. MVC-cshtml(条件编译已关闭)

    加单引号    

  7. [android] 与PHP的session进行交互demo

    从MainActivity跳转到MailIndexActivity,第一个请求接口设置session,第二个activity请求接口获取session java代码中获取header头里面的Set-C ...

  8. Eclipse 入手配置

    工欲善其事,必先利其器 在安装eclipse后进行简单的配置能够为以后的开发提高不少效率,这里慢慢先记录自己的经验. 一.编码配置 utf-8 1.worksplace: 首先设置就是workspac ...

  9. java导入excle表格,并且对表格进行相应的修改,并对表格数据进行整理,最后导出本地表格等一系列操作

    1.首先创建一个java项目 完成效果如下图所示 2.导入以下jar包 3.代码如下 其中行和列的操作是根据需求自动划分的 public class auto_date { private stati ...

  10. swagger2配置和使用

    1.导入swagger2 <dependency> <groupId>io.springfox</groupId> <artifactId>spring ...