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 ...
随机推荐
- solr windows 启动和关闭命令
进入bin目录执行 启动:solr -e dih 停止:solr stop -all D:\Solr\solr-5.2.1\solr-5.2.1\bin>solr -e dih
- System.exit(0)作用
System.exit(0)作用 public class HelloGoodbye{ try{ System.out.println(“Hello World”); System.exit(0) ...
- centos下载jdk
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com% ...
- css定位之绝对定位
绝对定位可以做很多事情,如广告位,弹出框,遮罩层等一些功能 css的定位方式:1.静态定位, 2.绝对定位(固定定位和绝对定位) ,3.相对定位 绝对定位会受到影响的因素有 1.属性的取值. 2.元素 ...
- volatile简介
volatile简介 java语言提供了一种稍弱的内存同步机制,即volatile变量.用来确保将变量的更新操作通知到其它线程,保证了新值能立即同步到主内存,以及每次使用前立即从内存刷新.当变量声明为 ...
- PureLayout和Masonry比较
一年前那时我做iOS开发,为了自动布局适配多种屏幕,我一般使用Masonry,后来偶然地在一个视频教程中发现老师使用了UIView+Autolayout(现在作者改名为PureLayout)自动布局, ...
- this面试题
// 考题1 /*function Fn() { console.log(this);//window } Fn(); new Fn();//Fn实例 Fn.apply(Fn); //将this指向F ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- C#代码示例_函数
参数数组 C#允许为函数指定一个(只能指定一个)特定的参数,这个参数必须是函数定义中的最后一个参数,称为参数数组.参数数组可以使用个数不定的参数调用函数,可以使用params关键字定义它们. 参数数组 ...
- 12-8下午 php语法
<?php //var_dump(empty($a)); //判断变量是否为空//var_dump(isset($a)); //判断变量是否定义//$a = 10;//unset($a); // ...