【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 ...
随机推荐
- Python3.6学习笔记(六)
WSGI Python Web Server Gateway Interface 规范学习 由于Python的灵活性,提供了多种方式可以作为服务端语言,包括Python编写的服务器(Medusa).P ...
- Coursera课程python中的一些程序
Index of /code Name Last modified Size Description Parent Directory - BeautifulSoup.py 07-Aug-2015 1 ...
- 混沌数学之拉比诺维奇-法布里康特方程(Rabinovich-Fabrikant equations)
拉比诺维奇-法布里康特方程(Rabinovich-Fabrikant equations)是 1979年苏联物理学家拉比诺维奇和法布里康特提出模拟非平衡介 质自激波动的非线性常微分方程组: dot{x ...
- 第九章 LinkedBlockingQueue源码解析
1.对于LinkedBlockingQueue需要掌握以下几点 创建 入队(添加元素) 出队(删除元素) 2.创建 Node节点内部类与LinkedBlockingQueue的一些属性 static ...
- 使用gitolite进行git服务器搭建
使用gitolite进行git服务器搭建 https://blog.csdn.net/pan0755/article/details/78460941 使用gitolite搭建,然后需要有个客户端进行 ...
- [INS-30131]执行安装程序验证所需的初始设置失败(原因:无法访问临时位置)
[INS-30131]执行安装程序验证所需的初始设置失败(原因:无法访问临时位置) 学习了:https://blog.csdn.net/killvoon/article/details/5182192 ...
- FrameWork逆向工程之MotioPI
在BI项目建设的过程中我们一般都是有备份的,而且这个是必须有的!特别是例如ETL Model,还有Data Model这一类的元数据,这些东西如果我们没有备份,而恰好的我们的开发模型又在某一天离我们而 ...
- Linux shell 脚本入门教程+实例
原文:http://www.wiquan.com/article/136 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是shell仍然是一个非常灵活的工具.She ...
- WPF代码模板-布局部分
Grid 两行和三列 <Grid ShowGridLines="True"> <Grid.RowDefinitions> <RowDefinition ...
- iOS用户体验之-导航之道
iOS用户体验之-导航之道 用户不会意识到有导航指向的存在除非他遇到非预期的效果. 能够说导航时逻辑跳转的节点.所以导航对用户体验是至关重要的. iOS中有三种类型的导航.每一种适合不同类型的app. ...