50. Pow(x, n) (recursion)
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)的更多相关文章
- LeetCode - 50. Pow(x, n)
50. Pow(x, n) Problem's Link ----------------------------------------------------------------------- ...
- [Leetcode][Python]50: Pow(x, n)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 50: Pow(x, n)https://leetcode.com/probl ...
- 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 ...
- LeetCode 50. Pow(x, n) 12
50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...
- Java实现 LeetCode 50 Pow(x,n)
50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...
- 刷题-力扣-50. Pow(x, n)
50. Pow(x, n) 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/powx-n/ 著作权归领扣网络所有.商业转载请联系官方授 ...
- [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 ...
- 50. Pow(x, n) (编程技巧)
Implement pow(x, n). double sum = 1; if (n > 0) { while ((n--) > 0) sum *= x; return sum; } el ...
- 50. Pow(x, n)
题目: Implement pow(x, n). 链接: http://leetcode.com/problems/powx-n/ 题解: 使用二分法求实数幂,假如不建立临时变量halfPow,直接r ...
随机推荐
- document.frames与window.frames在不同浏览器中的使用
问题: document.frames 只有 IE Opera 支持.等同于 window.frames.用来取得当前页面内 window 对象的集合. 在 Firefox Chorome Safar ...
- zato集群部署
注: SQL ODB和Cluster’s config需要首先依次创建,其他三个次序随意 对不熟悉的命令,使用server create *** -h 查看帮助文档 修改完后配置文件,要重启(zato ...
- 实现 如 goole closure 类似功能模块加载函数
看过goole closure 的同学都知道 其中定义一个类名函数时候只要 inlude("") 想加载某个模块只要require("")就可以利用: ...
- vuex 实现vue中多个组件之间数据同步以及数据共享。
http://pan.baidu.com/s/1hrJfpli demo下载地址 前言 在一些项目中有很多数据状态之间要实现数据共享状态共享,例如购物车的数据.用户的登录状态等等.vue父元素是可以 ...
- js分离html代码的body内外部分
//定义网页源码 str = '<!DOCTYPE html><html><head> <meta charset="UTF-8"> ...
- CentOS Linux 7.3 1611 (Core) 配置静态IP地址
详见: http://blog.csdn.net/johnnycode/article/details/50184073 设置静态IP 关于静态IP设置官方已经给出答案有兴趣的可以看官方WIKI指导, ...
- golang 使用rrd的相关资料
一.简介 RRDtool是指Round Robin Database工具,即环状数据库.从功能上说,RRDtool可用于数据存储+数据展示.著名的网络流量绘图软件MRTG和集群监控系统Gan ...
- 网站大于10M的视频不能播放
IIS配置的网站,添加了几个mp4视频,有个可以正常播放,有的却不加载不出来,提示错误: net::ERR_CONNECTION_ABORTED 网上有文章说是由于安全狗bug导致,下载安装一个补丁覆 ...
- pom文件解析
Maven的依赖是使用Maven坐标来定位的,而Maven坐标主要由GAV(groupId, artifactId, version)构成.因此,使用任何一个依赖之间,你都需要知道它的Maven坐标. ...
- js两个字符串明明一样却判断显示不相等
一.问题 两个字符串看起来一样.类型一样,判断str1==str2时返回false: 二.原因 字符串可能含有其他特殊字符:换行符(%D).空格(%20)...一般不显示. 三.如何判断 encode ...