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 ...
随机推荐
- c++ 网络编程(六)LINUX下 socket编程 多播与广播 实现一次发送所有组客户端都能接收到
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9614288.html 一.多播 锲子:有这么一种情况,网络电台可能需要同时向成千上万的用户传输 ...
- unity状态机实现
刚看了浅墨大神的文章让我对状态机有了进一步的理解 具体实现见装载的状态机文章 首先得有个总状态HeroineBaseState接口,其里面的方法主要是与行为相关的方法,让继承此接口的类来实现的 具体的 ...
- Mybatis 关联查询(三)
多对多的管理查询结果映射 1. 需求: 查询用户购买的商品信息 2. 分析: (1)用户和商品没有直接关联 (2)用户和订单进行了关联,订单和订单明细进行了关联,订单明细和商品进行了关联,因此 ...
- uni-app 页面配置和跳转(一)转
今天看Dcloud官网更新了个uni-app,据说一套代码三端发布(Android,iOS,微信小程序),果断一试. uni.navigateTo(OBJECT) 保留当前页面,跳转到应用内的某个页面 ...
- PyQt5 应用在 TeamViewer 下无法使用全屏模式
PyQt5 应用在 TeamViewer 下无法使用全屏模式 问题描述 使用 PyQt5 (版本为 5.7)中的 QtWebEngineView 构建的桌面应用,部署到远程机器(Windows 7 平 ...
- js设置下拉框选中后change事件无效解决
下拉框部分代码: <select id="bigType"> <option value="">请选择</option> & ...
- MyBatis 指定的转换无效
表字段Pay类型设置的是float,生成类的属性如下: public double Pay{get;set;} 读取列表时出现如下错误: 错误信息: 查看堆栈跟踪信息, get_Decimal()提示 ...
- ReentrantReadWriteLock简介
对象的方法中一旦加入synchronized修饰,则任何时刻只能有一个线程访问synchronized修饰的方法.假设有个数据对象拥有写方法与读方法,多线程环境中要想保证数据的安全,需对该对象的读写方 ...
- C#学习笔记14
1.在多个线程的同步数据中,避免使用this.typeof(type).string进行同步锁,使用这3个容易造成死锁. 2.使用Interlocked类:我们一般使用的互斥锁定模式(同步数据)为Lo ...
- java设计模式-观察者模式学习
最近学习了设计模式中的观察者模式,在这里记录下学习成果. 观察者模式,个人理解:就是一个一对多模型,一个主体做了事情,其余多个主体都可以观察到.只不过这个主体可以决定谁去观察他,以及做什么事情可以给别 ...