Implement pow(xn).

我的做法就比较傻了。排除了所有的特殊情况(而且double一般不可以直接判断==),然后常规情况用循环来做。- -|||

直接用循环,时间复杂度就比较大。应该是用二分法来做。先计算pow(x,n/2)。然后再pow(x,n)=pow(x,n/2)*pow(x,n/2)

class Solution {
public:
double power(double x, int n){
if(n==)
return ;
double v = power(x,n/);
if(n% == )
return v *v;
else
return v* v* x;
}
double myPow(double x, int n) {
if(n<)
return 1.0 / power(x,-n);
else
return power(x,n);
}
};
#define EPSINON 0.00001
#define Max 2147483647
#define Min -2147483648
#define DBL_MAX 1.7976931348623159e+308 class Solution {
public:
double myPow(double x, int n) {
/*
three special case
*/
if(n==)
return x;
if(n==)
return 1.0;
if(abs(x)==1.00000){
if(n%==)
return 1.0;
else
return x;
} if(n>=Max){
if(abs(x)<=EPSINON)
return 0.0;
else
return DBL_MAX;
} if(n<=Min){
if(abs(x)<=EPSINON)
return DBL_MAX;
else
return 0.0;
} int size=abs(n);
double tmp=x;
for(int i=;i<=size;i++){
tmp*=x;
}
if(n>=)
return tmp;
else
return 1.0/tmp;
}
};

[Leetcode]50. Pow(x, n)的更多相关文章

  1. [Leetcode][Python]50: Pow(x, n)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 50: Pow(x, n)https://leetcode.com/probl ...

  2. 【一天一道LeetCode】#50. Pow(x, n)

    一天一道LeetCode系列 (一)题目 Implement pow(x, n). (二)解题 题目很简单,实现x的n次方. /* 需要注意一下几点: 1.n==0时,返回值为1 2.x==1时,返回 ...

  3. 【LeetCode】50. Pow(x, n) 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 递归 迭代 日期 题目地址: https://le ...

  4. 【LeetCode】50. Pow(x, n) (3 solutions)

    Pow(x, n) Implement pow(x, n). 按照定义做的O(n)肯定是TLE的. 利用这个信息:x2n = (xn)2 有个注意点,当n为负是,直接取反是不可行的. 由于int的表示 ...

  5. 【Leetcode】50. Pow(x, n)

    Implement pow(x, n). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 O ...

  6. [LeetCode]Count and Say 计数和发言

    Count and Say 计数和发言 思路:首先要理解题意,可以发现后者是在前者的基础之上进行的操作,所以我们拿之前的结果作为现在函数的参数循环n-1次即可,接下来就是统计字符串中相应字符的个数,需 ...

  7. [LeetCode]152. Maximum Product Subarray

    This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...

  8. [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 ...

  9. LeetCode 50. Pow(x, n) 12

    50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...

随机推荐

  1. Java学习笔记之集合

    集合(Collection)(掌握) (1)集合的由来? 我们学习的是Java -- 面向对象 -- 操作很多对象 -- 存储 -- 容器(数组和StringBuffer) -- 数组而数组的长度固定 ...

  2. CSS3学习系列之布局样式(二)

    使用盒布局 在CSS3中,通过box属性来使用盒布局,例子如下: <!DOCTYPE html> <html lang="en"> <head> ...

  3. python 写csv文件

    一.只有一列内容: def create_file(self, a, b): # 上传csv 文件 # os.remove('openfile.csv') open_file = open('5000 ...

  4. ssh自动化出现的莫名报错

    代码如: ssh -q user@host <<EOF localhost EOF 会出现提示如: Pseudo-terminal will not be allocated becaus ...

  5. 网络配置之nmcli

    使用nmcli命令配置网络 NetworkManager是管理和监控网络设置的守护进程,设备既就是网络接口,连接是对网络接口的配置,一个网络接口可以有多个连接配置,但同时只有一个连接配置生效. 1 配 ...

  6. Educational Codeforces Round 25 Five-In-a-Row(DFS)

    题目网址:http://codeforces.com/contest/825/problem/B 题目:   Alice and Bob play 5-in-a-row game. They have ...

  7. Android项目导入工程Module

    在Android开发过程中,我们经常引用一些模块,或者自己封装好的Project.在Android Studio某个项目是可以引入多个Module的.这样导入Module的好处方便对源码修改以适合自己 ...

  8. hdu_1695: GCD 【莫比乌斯反演】

    题目链接 这题求[1,n],[1,m]gcd为k的对数.而且没有顺序. 设F(n)为公约数为n的组数个数 f(n)为最大公约数为n的组数个数 然后在纸上手动验一下F(n)和f(n)的关系,直接套公式就 ...

  9. 【linux相识相知】磁盘分区及文件系统管理详解

    磁盘,提供持久的数据存储,它不像我们的内存,如果突然断电了,在内存中的数据一般都会被丢掉了,内存中的数据在保存的时候,会被写到硬盘里面,磁盘也是一种I/O设备. 我们都知道磁盘分区完成之后,还要进行格 ...

  10. 导航栏使用UIButton自定义返回按钮的图片

    - (void)viewDidLoad { UIButton *backItem = [UIButton buttonWithType:UIButtonTypeCustom]; UIBarButton ...