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. 用泛型T替代object做为万能参数传递

    using System;using System.Collections;using System.Collections.Generic;using UnityEngine; public cla ...

  2. JavaScript弹出层

    1.这个弹出层就是一个DIV 2.看需要什么效果 2.1.如果是仅仅需要弹出层,而背后的网页同样可以点击,那么只需要一个DIV即可,效果如图: 2.2.需要一层透明背景,而后面的网页只能看不能点,效果 ...

  3. CCF 201409-4 最优配餐

    问题描述 试题编号: 201409-4 试题名称: 最优配餐 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 栋栋最近开了一家餐饮连锁店,提供外卖服务.随着连锁店越来越多,怎么 ...

  4. ckeditor添加代码插入功能及高亮显示(插件)

    Auto SyntaxHighlighter SyntaxHighlighter CKEditor Button 下载以上两个插件,启用 以下可有可无: (设置在编辑器的显示样式) ckeditor高 ...

  5. JS中的事件冒泡和事件捕获

    事件捕获阶段:事件从最上一级标签开始往下查找,直到捕获到事件目标(target). 事件冒泡阶段:事件从事件目标(target)开始,往上冒泡直到页面的最上一级标签. 用图示表示如下: 1.冒泡事件: ...

  6. Environment.Exit(0) 、Application.Exit() 、this.Close() 、this.Dispose()的区别

    Application.Exit:通知winform消息循环退出.程序会等待所有的前台线程终止后才能真正退出.是一种强行退出方式,就像 Win32 的 PostQuitMessage().它意味着放弃 ...

  7. Centos7 部署.netCore2.0项目

    最近在学习.netCore2.0,学习了在Centos上部署.netCore的方法,中间遇到过坑,特意贴出来供大家分享,在此我只是简单的在CentOS上运行.NETCore网站,没有运用到nginx等 ...

  8. 8、导航:Nav

    1.导航视图   angular2 中的是视图是显示在<router-outlet></router-outlet>里的同时他要依赖于 directives:[ ROUTER_ ...

  9. sublime text 2编辑器中文问题

    Sublime Text 2是一个非常不错的源代码及文本编辑器,但是不支持GB2312和GBK编码在很多情况下会非常麻烦.不过Sublime Package Control所以供的插件可以让Subli ...

  10. [生活] 日常英语学习笔记-NEVER HAVE I EVER游戏

    逛油管,看视频,学英语. 大家要过周末了说啥 Happy Sunday Have a restful  Sunday 有个空闲的周末 我们正在看电影 We are watching movie it ...