[Leetcode] powx n x的n次方
Implement pow(x, n).
题意:计算x的次方
思路:这题的思路和sqrt的类似,向二分靠近。例如求4^5,我们可以这样求:res=4、4*4^4。就是将每次在res的基础上乘以x本身,换成,每次以x*=x的方式前行,这样时间复杂度为O(logn),代码如下:
class Solution {
public:
double pow(double x, int n)
{
double res=1.0;
for(int i=n;i !=;i/=)
{
if(i% !=)
res*=x;
x*=x;
}
return n<?/res:res;
}
};
把x的n次方划分成两个x的n/2次方相乘,然后递归求解子问题,结束条件是n为0返回1。代码:
class Solution {
public:
double pow(double x, int n)
{
if(n==) return ;
double half=pow(x,n/);
if(n%==)
return half*half;
else if(n>)
return half*half*x;
else
return half*half/x;
}
};
[Leetcode] powx n x的n次方的更多相关文章
- [LeetCode] Super Pow 超级次方
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...
- [LeetCode] Power of Four 判断4的次方数
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...
- [LeetCode] Power of Three 判断3的次方数
Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...
- [LeetCode] Power of Two 判断2的次方数
Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...
- [LeetCode] Pow(x, n) 求x的n次方
Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...
- LeetCode 342. Power of Four (4的次方)
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- C#版(击败100.00%的提交) - Leetcode 372. 超级次方 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- [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 ...
- [LeetCode] 231. Power of Two 2的次方数
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...
随机推荐
- 一次IPC无法创建的问题
背景说明: 后台子系统都是运行在pc上的linux 系统有多个子系统,有一个子系统负责统一启停其他子系统,这里把这个子系统称为olddriver. ol ...
- editText设置最大长度
xml中可以设置为: <EditText android:layout_width = "fill_parent" android:layout_height = " ...
- MySQL日期比较
假如有个表product有个字段add_time,它的数据类型为datetime,有人可能会这样写sql: select * from product where add_time = '2013-0 ...
- es6笔记4^_^function
一.function默认参数 现在可以在定义函数的时候指定参数的默认值了,而不用像以前那样通过逻辑或操作符来达到目的了. es5 function sayHello(name){ //传统的指定默认参 ...
- 关于TensorFlow的GPU设置
摘自:https://blog.csdn.net/byron123456sfsfsfa/article/details/79811286 1. 在使用GPU版的TensorFlow跑程序的时候,如果 ...
- Leetcode - 461. Hamming Distance n&=(n-1) (C++)
1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...
- kvm网络虚拟化
网络虚拟化是虚拟化技术中最复杂的部分,学习难度最大. 但因为网络是虚拟化中非常重要的资源,所以再硬的骨头也必须要把它啃下来. 为了让大家对虚拟化网络的复杂程度有一个直观的认识,请看下图 这是 Open ...
- Samba共享权限分配
案例推荐:http://www.cnblogs.com/mchina/archive/2012/12/18/2816717.html 本文不详细介绍全部参数,只介绍完成需求的一些参数. 需求: 1,账 ...
- Thunder团队第六周 - Scrum会议2
Scrum会议2 小组名称:Thunder 项目名称:i阅app Scrum Master:宋雨 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...
- Notes of the scrum meeting(12.9)
meeting time:14:00~17:00p.m.,December 9th,2013 meeting place:一号教学楼209 attendees: 顾育豪 ...