LeetCode: Pow(x, n) 解题报告
Pow(x, n)
Implement pow(x, n).

SOLUTION 1:
使用二分法。
1. 负数的情况,使用以下的公式转化为求正数power,另外,考虑到MIN_VALUE可能会造成越界的情况,我们先将负数+1:
X^(-n) = X^(n + 1) * X
X^n = 1/(x^(-n))
2. Base case: pow = 0, RESULT = 1;
3. 正数的时候,先求n/2的pow,再两者相乘即可。
当n = -2147483648 必须要特别处理,因为对这个数取反会得到相同的数(已经越界)
所以当n为负时,先加个1再说。具体可以看代码。
先计算出x的n/2次方,再自乘一下。另外,注意n%2如果为1,
记得再乘以x
如果n是负数,所有计算完成后,执行x=1/x就行
public class Solution {
public double pow(double x, int n) {
if (x == 0) {
return 0;
}
// base case: when n = 0, the result is 1;
if (n == 0) {
return 1;
}
/*
递归的主体部分
*/
// X^(-n) = X^(n + 1) * X
// X^n = 1/(x^(-n))
if (n < 0) {
double ret = x * pow(x, -(n + 1));
return (double)1/ret;
}
// 将求pow对半分。再将结果相乘
double ret = pow(x, n / 2);
ret = ret * ret;
//如果有余数,再乘以x本身。
if (n % 2 != 0) {
ret = ret * x;
}
return ret;
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/divide2/Pow_1219_2014.java
LeetCode: Pow(x, n) 解题报告的更多相关文章
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
随机推荐
- java hibernate Criteria 删除数据 delete data 2种方法
public String deleteByUserAccount(String account) { 方式一: Session session = this.getCurrentSession(); ...
- IOCP笔记
IOCP是win32下的异步IO,利用线程池来异步处理IO请求. 这里要分析一下异步调用,跟同步调用不同,异步调用 调用了就马上返回,但是还留下个话:有事情了马上通知我,我会处理滴.恩恩,这很符合我的 ...
- 【LeetCode】174. Dungeon Game
Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...
- HTML <video> 标签
http://www.w3school.com.cn/tags/tag_video.asp <%@ Page Language="VB" AutoEventWireup=&q ...
- SQL Server 2008 R2占用内存越来越大两种解决方法
SQL Server 2008 R2运行越久,占用内存会越来越大. 第一种:有了上边的分析结果,解决方法就简单了,定期重启下SQL Server 2008 R2数据库服务即可,使用任务计划定期执行下边 ...
- linux 调用shell脚本传参
例子: boolean execResult = true; BufferedReader br = null; try { //lin ...
- 安卓listview滚动时背景变黑的解决方法
ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景图片,或者背景颜色时,滚动时listView会黑掉, 原因是,滚动时,列表里面的view重绘时,用的依 ...
- 最全PyCharm教程
最全PyCharm教程--for python PyCharm简介: PyCharm是由JetBrains打造的一款Python IDE,VS2010的重构插件Resharper就是出自JetBrai ...
- gRPC之Node Quick Start
在node环境下我们使用一个小例子作为引导: 在开始之前请确认如下: 1.node:版本在0.12以上 下载这个例子 为了更好地开始这个例子,你需要在本地对这个例子代码做一个备份.下载这个例子的代码从 ...
- 微信小程序如何获取屏幕宽度
微信小程序如何获取屏幕宽度 方法1: imageLoad: function () { this.setData({ imageWidth: wx.getSystemInfoSync().window ...