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. 题意:判断一个数是否是 ...
随机推荐
- jsp+urlrewrite实现html分页简单粗暴实现
今天去检查一同事写的一分页实现,看了有点郁闷,都说了要换成html实现方便搜索引擎收录,他还是用的js,真是晕.还得帮他解决 好吧.言归正传,我在他的基础上粗暴的修改了下,达到了基本的效果,带数字分页 ...
- 更改Sublimetext3的主题文件,改变某些不喜欢的颜色
使用的主题是Monokai(SL),主题很好看,但是注释和内容选中的颜色看起来跟没有一个样,看起来很淡,所以稍微改一下主题文件的颜色.
- PowerShell自定义对象
前面的性能高 使用[pscustomobject][ordered]强制类型转换 [pscustomobject][ordered]@{Name= 'Boe'Number = 1ID = 007} 使 ...
- SystemTimeToFileTime、FileTimeToLocalFileTime、LocalFileTimeToFileTime三函数的跨平台实现
// test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdlib.h> #include & ...
- JavaBean中DAO设计模式介绍(转)
一.信息系统的开发架构 客户层-------显示层-------业务层---------数据层---------数据库 1.客户层:客户层就是客户端,简单的来说就是浏览器. 2.显示层:JSP/Ser ...
- AS3性能及Flex-Formatting设置问题
1.支持Vector 2.for each in更是从Flash Player 9 3.Flash原生的bitmap.encode 4.如打开位图缓存:使用bitmapData.lock:把bitma ...
- 洛谷 P1731 生日蛋糕
/*洛谷 1731 生日蛋糕 傻傻的-1 T成了傻逼*/ #include<cstdio> #include<iostream> #include<cmath> # ...
- android应用一(调用WebServices)
搞了一个月的android,现学现卖,终于还是搞完了,停下来,整理思路,写写记录吧. 我们知道android访问远程数据库主要有两种协议,一种是SOAP,另外一种就是HTTP.而我们再看看WebSer ...
- 手机端的表单验证和PC端的不同
1.手机端:由于页面小的局限性,表单验证从上到下依次进行,如果上一个验证不通过,则给出错误提示,代码中return回去,不必进行下一个的校验: 2.PC端:页面范围大,一般是在表单的后面或者下面,提示 ...
- javascript技巧字典【转藏】
2015-12-31 js判断undefined类型 if (reValue== undefined){ alert("undefined"); } 发现判断不出来, ...