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: 输入: ...
随机推荐
- 【最短路】【数学】CSU 1806 Toll (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1806 题目大意: N个点M条有向边,给一个时间T(2≤n≤10,1≤m≤n(n-1), ...
- 使用vs2010编译 Python \ SIP \ PyQt4
(1)先使用vs2010编译 Python http://www.cnblogs.com/fortwo/archive/2013/04/16/3023871.html 注意,若编译的为debug版的P ...
- linux环境下deb格式 转换成rpm格式
linux环境下deb格式 转换成rpm格式 使用alien工具转换deb格式到rpm格式 alien_8.87.tar.gz 下载alien_8.87.tar.gz [root@mysqlnode2 ...
- zzuoj 10408: C.最少换乘【最短路dijkstra】
10408: C.最少换乘 Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 31 Solved: 8[Submit][Status][Web Board ...
- MySQL ERROR 1045错误解决办法
今天在安装MySQL数据库时,有安装过程中报ERROR 1045错误,网上查了一下,解决方法如下: 1.对于安装过程中该错选择"skip"继续向下安装,完成安装: 2.在MySQL ...
- Angular2学习
1.新建项目 2.新建Model public class TodoItem { public int Id { get; set; } public string Key { get; set; } ...
- AFNetworking (3.1.0) 源码解析 <六>
这次继续介绍文件夹Serialization下的类AFURLResponseSerialization.这次介绍就不拆分了,整体来看一下.h和.m文件. 协议AFURLResponseSerializ ...
- Gridview实现银行选择列表
[MainActivity.java] package com.example.activitydemo; import android.os.Bundle; import android.view. ...
- Shiro Quartz之Junit測试Session管理
Shiro的quartz主要API上提供了org.apache.shiro.session.mgt.quartz下session管理的两个类:QuartzSessionValidationJob和Qu ...
- Linux基本配置和管理 2 ---- Linux多命令协作----管道及重定向
1 管道和重定向 1 在Linux中大多数命令都很简单,很少出现复杂的命令,每个命令只是实现一个简单的功能,我们可以通过组合不同的命令来实现复杂的功能 2 在Linux中几乎所有的命令返回的数据都是纯 ...