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)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. LeetCode Power of Four

    原题链接在这里:https://leetcode.com/problems/power-of-four/ 题目: Given an integer (signed 32 bits), write a ...

  5. LeetCode Power of Three

    原题链接在这里:https://leetcode.com/problems/power-of-three/ 与Power of Two类似.检查能否被3整除,然后整除,再重复检查结果. Time Co ...

  6. Leetcode Power of Two

    Given an integer, write a function to determine if it is a power of two. 题目意思: 给定一个整数,判断是否是2的幂 解题思路: ...

  7. 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 ...

  8. LeetCode——Power of Two

    Description: Given an integer, write a function to determine if it is a power of two. public class S ...

  9. [LeetCode]Power of N

    题目:Power of Two Given an integer, write a function to determine if it is a power of two. 题意:判断一个数是否是 ...

随机推荐

  1. (转)解决png图片在IE6下不透明的方法

    来源于:http://xzl52199.blog.163.com/blog/static/95206446201142174540220/ 一.传统的JavaScript方法 思路: 1.一个专门解决 ...

  2. hdu 1199 Color the Ball(离散化线段树)

    Color the Ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  3. The Class Loader Hierarchy--转载

    Class loaders in the Application Server runtime follow a delegation hierarchy that is illustrated in ...

  4. .NET 解析HTML代码——NSoup

    NSoup是一个开源框架,是JSoup(Java)的.NET移植版本 1.直接用起来 NSoup.Nodes.Document htmlDoc = NSoup.NSoupClient.Parse(HT ...

  5. Android 开发之自定义Dialog及UI的实现

    我们在开发中,经常会自定义Dialog,因为原生的AlertDialog无法满足我们的需求,这个时候就需要自定义Dialog,那么如何自定义呢,其实不难,就是有点繁琐而已.也就是自定义一个UI的xml ...

  6. guid 新建

    var guid = Guid.NewGuid();foreach (var i in new string[] { "P", "N", "B&quo ...

  7. Java-Android 之动画的实现

    一:显示隐藏动画 在res目录下创建一个anim目录,然后在里面创建一个alpha.xml文件 <?xml version="1.0" encoding="utf- ...

  8. [Mime] QuotedPrintableEncoding帮助类 (转载)

    点击下载 QuotedPrintableEncoding.rar 这个类是关于QuotedPrintableEncoding的帮助类看下面代码吧 /// <summary> /// 类说明 ...

  9. “jni.h”: No such file or directory

    VS2010解决方案: 进入 “包含目录“ 方式: 右键项目属性页-> 配置属性->VC++目录->包含目录 在”包含目录“中编辑 添加以下路径: C:\Program Files\ ...

  10. DataList分页-增加自动编号列

    <asp:DataList ID="dl_XUDAXIA" runat="server"> <HeaderTemplate> <t ...