leetcode power(x,n)
class Solution {
public:
double pow(double x, int n)
{
double a=1;
if(n==0)return 1;
if(x==1)return 1;
if(x==-1)
{
if(n&1)return -1;
else return 1;
}
int absn=abs(n);
a=power(x,absn);
if(n<0)
{
a=1/a;
}
return a;
}
double power(double x,int n)
{
double result=1;
if(n==1)return x;
if(n&1)
{
result*=x;
n=n-1;
}
double temp=power(x,n>>1);
result=result*temp*temp;
return result;
}
};
leetcode power(x,n)的更多相关文章
- [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 Power of Four
原题链接在这里:https://leetcode.com/problems/power-of-four/ 题目: Given an integer (signed 32 bits), write a ...
- LeetCode Power of Three
原题链接在这里:https://leetcode.com/problems/power-of-three/ 与Power of Two类似.检查能否被3整除,然后整除,再重复检查结果. Time Co ...
- Leetcode Power of Two
Given an integer, write a function to determine if it is a power of two. 题目意思: 给定一个整数,判断是否是2的幂 解题思路: ...
- Leetcode Power of two, three, four
Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...
- LeetCode——Power of Two
Description: Given an integer, write a function to determine if it is a power of two. public class S ...
- [LeetCode]Power of N
题目:Power of Two Given an integer, write a function to determine if it is a power of two. 题意:判断一个数是否是 ...
随机推荐
- SSH开源框架考试题
一.选择题 1.不属于Action接口中定义的字符串常量的是____B___. A.SUCCESS B.FAILURE C.ERROR ...
- 怎样克服 JavaScript 框架疲劳?
[编者按]Tero Parviainen 著有 Build Your Own AngularJS,曾两次组织 Clojure CUP 竞赛,在 Twitter 上有近两千名关注者. 在本文中.Tero ...
- Android开发优化之——对Bitmap的内存优化
http://blog.csdn.net/arui319/article/details/7953690 在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitm ...
- [TypeScript] Configuring TypeScript Which Files to Compile with "Files" and "OutDir"
This lesson shows how to configure the .tsconfig so you only compile the .ts files you want. It then ...
- android83 Activity的生命周期,启动模式,返回时传递数据
#Android四大组件 * Activity * BroadCastReceiver * Service * ContentProvider #Activity生命周期 * oncreate:Act ...
- 24小时学通LINUX内核系列
http://www.cnblogs.com/lihuidashen/category/667475.html
- allocator例子
13.39 编写自己的StrVec,包括自己版本的reserve.capacity和resize. 13.40 为StrVec添加一个构造函数,它接受一个initializer_list<str ...
- LabVIEW设计模式系列——功能全局变量
标准化:1.图标的标准化 2.模式的标准化Operation:Write & Read 3.xx为变量名称,w为write括号中为默认值,r为read ...
- Linux下搭建Oracle11g RAC(7)----安装Oracle 软件
从此步骤开始,我们正式安装oracle软件: ① 以oracle用户登录图形界面,执行/home/oracle/database/runInstaller,进入OUI的图形安装界面: ② 进入OUI安 ...
- Android(java)学习笔记172:BroadcastReceiver之 Android广播机制
Android广播机制 android系统中有各式各样的广播,各种广播在Android系统中运行,当"系统/应用"程序运行时便会向Android注册各种广播.Android接收到广 ...