50 Pow(x, n)(求x的n次方Medium)
2015-06-13 01:51
260 查看
题目意思:x为double,n为int,求x的n次方
思路分析:直接求,注意临界条件
时间复杂度:O(n)
运行时间:20ms
此题并没有用什么算法,有时间再研究
思路分析:直接求,注意临界条件
class Solution { public: double myPow(double x, int n) { if(x==1.0)return x; else if(x==-1.0){ if(n%2==0)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>0)return ans; else return 1.0/ans; } };
时间复杂度:O(n)
运行时间:20ms
此题并没有用什么算法,有时间再研究
相关文章推荐
- JAVA引用DLL文件方法
- Android 使用Fiddler对应用进行抓包(一)
- 自定义维度与指标
- C++在VS2008的IDE环境中操作Excel2007(基础)
- c语言常见错误与注意点
- 多渠道打包build.gradle收藏版
- iOS 中间显示透明圆
- Mysql 分区 ------ Innodb 表引擎
- Gradle代码混淆文件个人收藏版
- 自制MyEclipse豆沙绿主题
- 开启微软机器学习之旅(1)--如何从不同数据源将样本数据导入Azure Machine Learning Studio
- PHP生成缩略图、加水印
- 整理关于css中position与float的一些用法
- MachineLearning Exercise 4 :Neural Networks Learning
- networking常用命令
- 34 Search for a Range(目标数的范围Medium)
- 初识flask——数据库(2015.6.13)
- [葫芦娃的成长之路]数据结构的预备知识
- 定位未导出的函数地址(SHCreateProcess)
- java 为何会出现框架