50 Pow(x, n)(求x的n次方Medium)
题目意思:x为double,n为int,求x的n次方
思路分析:直接求,注意临界条件
class Solution {
public:
double myPow(double x, int n) {
if(x==1.0)return x;
else if(x==-1.0){
if(n%==)return 1.0;
else return -1.0;
}
double ans=1.0;
int flag=abs(n);
while(flag--&&abs(ans)>0.0000001)ans*=x;
if(n>)return ans;
else return 1.0/ans;
}
};
时间复杂度:O(n)
运行时间:20ms
此题并没有用什么算法,有时间再研究
50 Pow(x, n)(求x的n次方Medium)的更多相关文章
- [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] Pow(x, n) 求x的n次方
Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...
- [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 ...
- [LintCode] Pow(x, n) 求x的n次方
Implement pow(x, n). Notice You don't need to care about the precision of your answer, it's acceptab ...
- leetcode 50. Pow(x, n) 、372. Super Pow
50. Pow(x, n) 372. Super Pow https://www.cnblogs.com/grandyang/p/5651982.html https://www.jianshu.co ...
- LeetCode - 50. Pow(x, n)
50. Pow(x, n) Problem's Link ----------------------------------------------------------------------- ...
- [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)
Implement pow(x, n). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 O ...
- LeetCode 50. Pow(x, n) 12
50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...
随机推荐
- 去掉cell边框的简单办法
实很简单,把backgroundView设置为一个空的View,然后就干净了 UIView *tempView = [[UIView alloc] init]; [cell setBackground ...
- log4cxx在vs2013的静态编译
网络上找了一圈,结果都是通过修改代码来编译,做为强迫症患者接受不了这种修改代码却无法预知代码带来影响的方式,而且没有静态编译的方法,为了方便其他人后续不在走弯路,提供自己的编译方法. 虽然最终的目的是 ...
- Cyclic Nacklace - HDU 3746(next求循环节)
题目大意:给你一些串,问如果想让这个串里面的循环节至少循环两次,需要添加几个字符(只能在最前面或者最后面添加).比如ababc 需要添加5个就是添加ababc. 分析:其实字符串的长度len-next ...
- 其实H5制作就这样~
今天的H5技术越来越成熟了,H5制作变得越来越简单化,尤其是H5制作工具(http://wcd.im/?_ta=2787)领域,基本上已经形成一条比较完整的产业链,所以对当下的企业来说,如果仍未体验过 ...
- nginx 代理概念理解
学习nginx http://www.nginx.cn/nginx-how-to nginx官方定义: Nginx 是一个高性能的 Web 和反向代理服务器. 首先明确一个概念 反向代理 反向代理(R ...
- idea unknow facet type web 解决方案
菜单 -->Preferences-->Plugins 添加tomcat支持 如图: 然后 项目project-setting中 可以添加 web类型的facets了 pasting
- Unity3D 画线插件 Vectrosity_Simple2DLine
Vectrosity是一个很方便的画线插件,用它我们可以画出2D,3D,贝塞尔,圆,椭圆等各种线条图案. :链接: http://pan.baidu.com/s/1pJjTFjt 密码: uesn 首 ...
- QML官方系列教程——QML Applications
附网址:http://qt-project.org/doc/qt-5/qmlapplications.html 假设你对Qt的官方demo感兴趣,能够參考本博客的另一个系列Qt5官方demo解析集 每 ...
- 獲取 Textarea 的光標位置(摘自網絡)
在任何编辑器中,获取光标位置都是非常重要的,很多人可能认为较难,其实只要处理好浏览器的兼容,还是比较容易实现的.下面我们一起来看看如何获取到 Textarea 元素中的光标位置.首先,我们用 rang ...
- [转] [翻译]图解boost::bind
http://kelvinh.github.io/blog/2013/12/03/boost-bind-illustrated/ 其实这是很久之前留的一个坑了,一直没有填.. 记得在刚开始看到 boo ...