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 ...
随机推荐
- Java设计模式系列3--抽象工厂模式(Abstract Factory Method)
2014-02-26 10:55:47 声明:本文不仅是本人自己的成果,有些东西取自网上各位大神的思想,虽不能一一列出,但在此一并感谢! 工厂方法模式有一个问题就是,类的创建依赖工厂类,也就是说,如果 ...
- 理解RESTful架构(转载)
本文转载自:http://www.ruanyifeng.com/blog/2011/09/restful.html 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软 ...
- 《大象-Think In UML》读书笔记1
大音希声,大象希行. 什么是面向过程?什么是面向对象? 面向过程归纳为结构化程序设计.DFD图.ER模型.UC矩阵等,而面向对象则被归纳为继承.封装.多态.复用等具体的技术.事实上,上述的所有技术都只 ...
- iOS系统消息
一.键盘1.UIKeyboardWillShowNotification-将要弹出键盘2.UIKeyboardDidShowNotification-显示键盘3.UIKeyboardWillHideN ...
- Win7下通过eclipse远程连接CDH集群来执行相应的程序以及错误说明
最近尝试这用用eclipse连接CDH的集群,由于之前尝试过很多次都没连上,有一次发现Cloudera Manager是将连接的端口修改了,所以才导致连接不上CDH的集群,之前Apache hadoo ...
- Objective-C学习笔记-第三天(1)
今天开始用oc写iOS程序,遇到的问题有 1.在不同的类使用类的方法或者访问类的属性的时候(公开的方法或者属性),方法或者属性必须在类头文件中声明. 2.对象类型的声明以及定义需要用*,表明这个是一个 ...
- JS 功能弹框封装
// 功能提示弹框 function messageBox ( option ) { var html = ''; html += '<div class="message-box-h ...
- CSS 第四天 多重背景 变形 过渡
背景原点:background-origin 图片起始位置 border-box包括边框 padding-box边框内 content-box 内容内 **background-repeat 为no- ...
- 黑马程序员——【Java基础】——泛型、Utilities工具类、其他对象API
---------- android培训.java培训.期待与您交流! ---------- 一.泛型 (一)泛型概述 1.泛型:JDK1.5版本以后出现的新特性,用于解决安全问题,是一个类型安全机制 ...
- JS中 obj.style.left 与 obj.offsetLeft 的区别
1.obj.style.left 取得是字符串:28px; 而 obj.offsetLeft取得的值为数字 28: 2.obj.style.left 需要事先将left值写在HTML中,不能致谢在CS ...