LeetCode Implement pow(x, n).
这个题目我也没有思路,同学们可以查看这个http://www.cnblogs.com/NickyYe/p/4442867.html
下面是我改进后的代码
第一种方法:
class Solution {
public:
double myPow(double x, int n) {
if ( == n)return ;
double half = myPow(x, n / );
if (!(n % ))
return half*half;
else if (n>)
{
return half*half*x;
}
else
{
return half*half*( / x);
}
}
};
第二种方法:
if ( == n)return ;
double result = 1.0;
bool tag = false;
double num=1.0;
if(n==-)
{
n=+n;
num=x;
}
if (n<)
{
n = -n;
tag = true;
}
while (n)
{
if (n & )
{
result *= x;
}
x = x*x;
n = n >> ;
}
if (tag)
{
if(0.999999<num&&num<1.000001)
return / result;
else return /result*num;
}
if(0.999999<num&&num<1.000001)
return result;
else return result*num;
}
};
LeetCode Implement pow(x, n).的更多相关文章
- [LeetCode 题解]: pow(x,n)
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Implement po ...
- [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 ...
- Java for LeetCode 050 Pow(x, n)
Implement pow(x, n). 解题思路: 直接使用乘法实现即可,注意下,如果n很大的话,递归次数会太多,因此在n=10和n=-10的地方设置一个检查点,JAVA实现如下: static p ...
- [leetcode]50. Pow(x, n)求幂
Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Ou ...
- leetCode 50.Pow(x, n) (x的n次方) 解题思路和方法
Pow(x, n) Implement pow(x, n). 思路:题目不算难.可是须要考虑的情况比較多. 详细代码例如以下: public class Solution { public doubl ...
- leetcode 二分法 Pow(x, n)
Pow(x, n) Total Accepted: 25273 Total Submissions: 97470My Submissions Implement pow(x, 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] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- bzoj 3122: [Sdoi2013]随机数生成器
#include<cstdio> #include<iostream> #include<map> #include<cmath> #define ll ...
- 通过top命令发现plymouthd进程cpu负载达到近100% 解决办法
最近几天一直遇到服务器cpu100%, 通过top命令发现plymouthd进程cpu负载达到近100% 解决方法:打开 /boot/grub/menu.lst , 去掉 “rhgb quiet”这两 ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- 解决 iOS7 通过tag 找不到 UITableViewCell 的子控件
当iOS7问世,程序的世界就混乱了,以前良好的程序,现在是一塌糊涂,于是只能把问题一个一个攻破. 由于项目当中需要每个cell显示数目不同的图片,于是我在每个cell 赋值之前,通过一下方法把cell ...
- Qt msgBox 快捷版
使用 int iRet = msgBox(pos, tr("警告") , tr("确定要删除当前选中的行吗?") , tr(, ); == iRet) retu ...
- 2014年4月底至5月初51Aspx源码发布详情
精灵豆会员管理系统源码 2014-4-21 [VS2010]功能介绍:精灵豆会员管理系统业务管理平台采用微软选进的C#语言开发,采用大型数据库,具有比较高的执行效率和高安全性.系统分为消费管理,会员 ...
- Hadoop随笔(一):工作流程的源码
一.几个可能会用到的属性值 1.mapred.map.tasks.speculative.execution和mapred.reduce.tasks.speculative.execution 这两个 ...
- Maven工程中的右键team
与资源库同步(S):在需要合并版本时使用 提交(C):本地代码写入源码库 更新(U):本地代码升级到服务器端版本 在点击更新时,请注意: 如果当前项目有改动(甚至是比原来多了一个空格),则此时无法更新 ...
- CI整合Smarty
1.到相应的站点下载smarty模板: 2.将源代码中的libs目录复制到项目的libraries目录下,改名为smarty3.0 3.在项目目录的libraries文件夹内新建文件ci_smarty ...
- python数据结构与算法——完全树 与 最小/大堆
# 完全树 最小堆 class CompleteTree(list): def siftdown(self,i): """ 对一颗完全树进行向下调整,传入需要向下调整的节 ...