[Leetcode]50. Pow(x, n)
Implement pow(x, n).
我的做法就比较傻了。排除了所有的特殊情况(而且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)的更多相关文章
- [Leetcode][Python]50: Pow(x, n)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 50: Pow(x, n)https://leetcode.com/probl ...
- 【一天一道LeetCode】#50. Pow(x, n)
一天一道LeetCode系列 (一)题目 Implement pow(x, n). (二)解题 题目很简单,实现x的n次方. /* 需要注意一下几点: 1.n==0时,返回值为1 2.x==1时,返回 ...
- 【LeetCode】50. Pow(x, n) 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 递归 迭代 日期 题目地址: https://le ...
- 【LeetCode】50. Pow(x, n) (3 solutions)
Pow(x, n) Implement pow(x, n). 按照定义做的O(n)肯定是TLE的. 利用这个信息:x2n = (xn)2 有个注意点,当n为负是,直接取反是不可行的. 由于int的表示 ...
- 【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 ...
- [LeetCode]Count and Say 计数和发言
Count and Say 计数和发言 思路:首先要理解题意,可以发现后者是在前者的基础之上进行的操作,所以我们拿之前的结果作为现在函数的参数循环n-1次即可,接下来就是统计字符串中相应字符的个数,需 ...
- [LeetCode]152. Maximum Product Subarray
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- [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 ...
- LeetCode 50. Pow(x, n) 12
50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...
随机推荐
- SQLite的总结与在C#的使用
这几天接触了一下SQLite,算是有点收获吧,因此总结一下. SQLite简介: SQLite是用C语言编写的数据库引擎,可以运行在Linux.Windows.Mac平台上. SQLite安装简单,下 ...
- Python3中文件处理
1.txt,xls,doc等文件的使用 f=open("filename","w") 打开一个用于写入的文件,要写入内容时使用f.write("内 ...
- usaco training 4.1.2 Fence Rails 题解
Fence Rails题解 Burch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of h ...
- 如何在java中用Arraylist中实现冒泡排序的问题
众所周知,冒泡排序法在一般数组中就3步, if(a<b){ temp=a; a=b; b=temp; } 然而,在集合中就不是简单的交换一下了,因为交换之后,必须保证新的值被重新设置到集合中去. ...
- ArcGisEngine图层操作(随笔,不全)
1.加载图层: 1.1 object.AddLayer(Layer[,toindex=0]) Layer表示ILayer对象,必选,toIndex参数表示图层索引(长整型),没需求可以忽略. 1.2 ...
- 智联招聘 卓聘IM演进过程
1. 卓聘IM开发背景 智联卓聘是智联旗下高端人才招聘平台,成立快4年了,业务增涨每年以100%速度增涨,业务增涨快在开发和上线速度要求也比较高. 2016年6月提出IM开发需求,7月初上线,开发人 ...
- 使用TinyXML进行XML操作
本例基于TinyXML实现XML的自动解析和创建,由于本人是菜鸟刚入门,例子中添加了enum.struct.vector.map.list的常见用法,首先添加6个tinyxml工程文件,然后设置调试参 ...
- 第2章 rsync(二):inotify+rsync详细说明和sersync
本文目录: inotify+rsync 1.1 安装inotify-tools 1.2 inotifywait命令以及事件分析 1.3 inotify应该装在哪里 1.4 inotify+rsync示 ...
- SpringMVC源码情操陶冶-ViewResolver视图解析
简单分析springmvc是如何解析view视图,并返回页面给前端 SpringMVC配置视图解析器 <bean id="viewResolver" class=" ...
- maven创建web工程Spring配置文件找不到问题解决方案
使用maven创建web工程,将Spring配置文件applicationContext.xml放在src/resource下,用eclipse编译时提示class path resource [ap ...