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 ...
随机推荐
- genymotion模拟器相关知识
最近学react用到了Genymotion模拟器,但是因为墙的原因,遇到了很多阻碍,例如以下几种: 这里分享一个手动下载模拟器的方法. 1. 找到以下路径:C:\Users\Administrato ...
- eclipse 闪退
在eclipse.ini文件中加入 -Dorg.eclipse.swt.browser.DefaultType=mozilla
- 学习Django
1.安装 命令:pip install Django 安装慢且有异常:HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed ...
- C#导出excel
public FileResult ExportExcels(string StartTime, string EndTime, string SendType) { List<SMSSExpo ...
- Jenkins快速上手
一.Jenkins下载安装 1.到官网下载jenkins.war包:http://jenkins-ci.org/ 2.安装方法有两种: a) 把下载下来的jenkins.war包放到文件夹下,如C:\ ...
- SQL Server2012关于表内事项出现次数降序排列(存储过程)
USE [growup] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[S_GetRanking ...
- IEEE参考文献格式
IEEE参考文献格式 由于国外期刊参考文献与国内参考文献的格式有很大区别,其中最常用的参考文献为会议论文.书籍.期刊文献,所以特别在此记录说明,方便以后使用. 会议论文(Published Confe ...
- Python学习资料整理以及书籍、开发工具推荐
我不知道大家学习Python的时候是不是和我一样感觉很无助,不知道在入门或者进阶的时候应该掌握哪些知识点,下面我就梳理下我自己学习Python开 发的过程及资料分享给大家,这些方法资料可能并不适合所有 ...
- CSS中相对定位与绝对定位
看了几个讲解定位的博客,觉得还不错,分享之: 博客一:http://blog.sina.com.cn/s/blog_4bcf4a5e010008o0.html 文章中,主要需要参考的有两点: 1,相对 ...
- SVG 2D入门11 - 动画
交互性 SVG拥有良好的用户交互性,例如:1. SVG能响应大部分的DOM2事件.2. SVG能通过cursor良好的捕捉用户鼠标的移动.3. 用户可以很方便的通过设置svg元素的zoomA ...