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: 输入: ...
随机推荐
- ZOJ 3469 Food Delivery
题目大意: 有n个人,住在一条直线上.第i个人的坐标是Xi,街上有个外卖餐馆的位置是X,现在餐厅工作人员要给街上的每个人送饭,送完之后再回到餐厅,送饭人的速度是V,每个人有个不满意值,当这个人送餐时间 ...
- 【转】Java删除文件夹和文件
原文网址:http://kxjhlele.iteye.com/blog/323657 以前在javaeye看到过关于Java操作文件的一篇文章,写的很好,但找了半天也没找到,就把找到底几篇文章整理一下 ...
- (转载)eclipse 快捷键大全,eclipse查找类,文件,添加注释
(转载)http://hi.baidu.com/fegro/item/8224c8c28b174627ee466598 /* ----------------------------------- ...
- 数位DP:SPOJ KPSUM - The Sum
KPSUM - The Sum One of your friends wrote numbers 1, 2, 3, ..., N on the sheet of paper. After that ...
- 【模拟】Codeforces 705A Hulk
题目链接: http://codeforces.com/problemset/problem/705/A 题目大意: 给一个数N(N<=100),N=1时输出"I hate it&qu ...
- Oracle用户管理
创建用户 概述:在oracle中要创建一个新的用户使用create user 语句,一般是具有dba(数据库管理员)的权限才能使用. create user 用户名 identified by 密码: ...
- 无题II hdu 2236(二分枚举区间)
分析:只需要用二分找一个区间,然后不断枚举这个区间是否可以达到最大匹配,一直二分到答案为止. 代码: =============================================== ...
- MySQL ERROR 1045错误解决办法
今天在安装MySQL数据库时,有安装过程中报ERROR 1045错误,网上查了一下,解决方法如下: 1.对于安装过程中该错选择"skip"继续向下安装,完成安装: 2.在MySQL ...
- Android--图片的三级缓存策略
三级缓存缓存(内存)--->本地磁盘---->网络1.首先看一下图片存储到本地磁盘 public class FileUtils { String path;//文件存储的地方 publi ...
- poj 3422 (费用流)
从左上角到有下角k次能获得的最大值. 跟hdu 2686一样的题目,这题一个点可以重复走,只能得到一次值. #include<stdio.h> #include<string.h&g ...