【LeetCode】50. Pow(x, n) (3 solutions)
Pow(x, n)
Implement pow(x, n).
按照定义做的O(n)肯定是TLE的。
利用这个信息:x2n = (xn)2
有个注意点,当n为负是,直接取反是不可行的。
由于int的表示范围是[2-31, 231-1],当n为INT_MIN时,取反会溢出。
因此需要对n==INT_MIN单独考虑。
另外,除以2可以用右移1位来实现。
解法一:递归
class Solution {
public:
double pow(double x, int n) {
if(n < )
{
if(n == INT_MIN) //-INT_MIN will cause overflow
return pow(x, n+)/x;
else
{
x = /x;
n = -n;
}
}
return Helper(x, n);
}
double Helper(double x, int n)
{//n > 0
if(n == )
return ;
double partRes = Helper(x, n>>);
if(n% == )
return partRes*partRes*x;
else
return partRes*partRes;
}
};

解法二:非递归
对于n的二进制表示,考虑每一位的0/1。
举例n==5,二进制表示为101
右数第一位为1,需要乘以x
右数第二位为0,不需要乘以x2
右数第三位为1,需要乘以x4
class Solution {
public:
double pow(double x, int n) {
if(n == )
return ;
int sign = ;
if(n == INT_MIN)
return pow(x, n+) / x;
else if(n < )
{
sign *= -;
n *= -;
}
double ret = ;
double mul = x;
while(n)
{
if(n & )
ret *= mul;
n >>= ;
mul *= mul;
}
if(sign == )
return ret;
else
return / ret;
}
};

解法三:just a joke
class Solution {
public:
double pow(double x, int n) {
return std::pow(x,n);
}
};

【LeetCode】50. Pow(x, n) (3 solutions)的更多相关文章
- 【LeetCode】50. Pow(x, n) 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 递归 迭代 日期 题目地址: https://le ...
- 【Leetcode】50. Pow(x, n)
Implement pow(x, n). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 O ...
- 【一天一道LeetCode】#50. Pow(x, n)
一天一道LeetCode系列 (一)题目 Implement pow(x, n). (二)解题 题目很简单,实现x的n次方. /* 需要注意一下几点: 1.n==0时,返回值为1 2.x==1时,返回 ...
- 【LEETCODE】50、数组分类,简单级别,题目:888,1013,896,485,448,697
package y2019.Algorithm.array; import java.util.HashSet; import java.util.Set; /** * @ProjectName: c ...
- 【LeetCode】12. Integer to Roman (2 solutions)
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【LeetCode】234. Palindrome Linked List (2 solutions)
Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could ...
- 【LeetCode】206. Reverse Linked List (2 solutions)
Reverse Linked List Reverse a singly linked list. click to show more hints. Hint: A linked list can ...
- 【LeetCode】200. Number of Islands (2 solutions)
Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
随机推荐
- python——获取数据类型:type()、isinstance()的使用方法:
python——获取数据类型 在python中,可使用type()和isinstance()内置函数获取数据类型 如: (1)type()的使用方法: >>> a = '230' ...
- Informatica 常用组件Lookup之三 关系和平面文件查找
创建查找转换时,您可以选择使用关系表或平面文件作为查找源. 关系查找 使用关系表作为查找源来创建查找转换时,您可以使用 ODBC 连接到查找源并导入表定义作为查找转换的结构. 仅可对关系查找使用以下选 ...
- Linked List Cycle leetcode java (链表检测环)
题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin ...
- IE6与CSS样式兼容问题汇总
1.PNG半透明图片的问题 虽然可以通过JS等方式解决,但依然存在载入速度等问题,所以,这个上能不要用还是尽量不要用.以达到网站最大优化. 2.IE6下的圆角 IE6不支持CSS3的圆角属性,比较好的 ...
- 上传文件multipart form-data boundary 说明
含义 ENCTYPE="multipart/form-data" 说明: 通过 http 协议上传文件 rfc1867协议概述,客户端发送内容构造. 概述 ...
- Logistic Regression的几个变种
原文:http://blog.xlvector.net/2014-02/different-logistic-regression/ 最近几年广告系统成为很多公司的重要系统之一,定向广告技术是广告系统 ...
- 实战:INNOBACKUPEX for mysql 5.6自己主动还原脚本-v2
脚本再次更新,共享一下! #!/bin/sh # # 用法: # ./restore.sh /你备份文件的全路径 #ocpyang@126.com INNOBACKUPEX=innobackupex ...
- Immediately-Invoked Puzzler
The Poplar Puzzle-makers weren’t too impressed. They barely noticed your simple and beautiful array ...
- (LeetCode 83)Remove Duplicates from Sorted Lists
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 强大的json工具:fastJson
fastJson FastJSON是一个很好的java开源json工具类库,相比其他同类的json类库,它的速度的确是fast,最快!但是文档做得不好,在应用前不得不亲测一些功能. 实际上其他 ...