428. Pow(x, n)【medium】
Implement pow(x, n).
Notice
You don't need to care about the precision of your answer, it's acceptable if the expected answer and your answer 's difference is smaller than 1e-3.
Pow(2.1, 3) = 9.261
Pow(0, 1) = 0
Pow(1, 0) = 1
O(logn) time
参考了@grandyang 的代码
解法一:
class Solution {
public:
double myPow(double x, int n) {
double res = 1.0;
for (int i = n; i != ; i /= ) {
if (i % != ) {
res *= x;
}
x *= x;
}
return n < ? / res : res;
}
};
迭代
解法二:
class Solution {
public:
double myPow(double x, int n) {
if (n < ) {
return / power(x, -n);
}
return power(x, n);
}
double power(double x, int n) {
if (n == ) {
return ;
}
double half = power(x, n / );
if (n % == ) {
return half * half;
}
return x * half * half;
}
};
解法三:
class Solution {
public:
/**
* @param x the base number
* @param n the power number
* @return the result
*/
double myPow(double x, int n) {
if (n == ) {
return ;
}
if (n == ) {
return x;
}
if (n == -) {
return / x;
}
return myPow(x, n / ) * myPow(x, n - n / );
}
};
会超时
428. Pow(x, n)【medium】的更多相关文章
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- 92. Reverse Linked List II【Medium】
92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- 61. Search for a Range【medium】
61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...
- 62. Search in Rotated Sorted Array【medium】
62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...
- 74. First Bad Version 【medium】
74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- 159. Find Minimum in Rotated Sorted Array 【medium】
159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...
- Java for LeetCode 207 Course Schedule【Medium】
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- HTML二(基本标签)
一.标题 HTML 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. <!--标题--> <h1>标题 1</h1> ...
- hdu4059The Boss on Mars 容斥原理
//求1到n之间与n互质的数的四次方的和 //segma(n^4) = (6n^5+15n^4+10n^3-n)/30 //对于(a/b)%mod能够转化为(a*inv(b))%mod //inv(b ...
- 解决Unable to load component class org.sonar.scanner.report.ActiveRulesPublisher/Unable to load component interface org.sonar.api.batch.rule.ActiveRules: NullPointerException
解决办法 Delete the directory data/es in your SonarQube installation. Restart SonarQube.
- Cocos2d-x3.0 iOS 一键编译多个target并打包ipa。
1.编写app打包为ipa的 shell脚本.将以下代码保存为app2ipa.sh. #!/bin/sh m_appPath="" m_ipaPath="" m ...
- JSON path
https://github.com/itguang/gitbook-smile/blob/master/springboot-fastjson/fastjson%E4%B9%8BJSONPath%E ...
- exception PLS-00403: expression 'V_END' cannot be used as an INTO-target of a SELECT/FETCH statement
exception PLS-00403: expression 'V_END' cannot be used as an INTO-target of a SELECT/FETCH stateme ...
- 理解并使用.NET 4.5中的HttpClient(转)
原文地址:http://www.cnblogs.com/wywnet/p/httpclient.html HttpClient介绍HttpClient是.NET4.5引入的一个HTTP客户端库,其命名 ...
- More Effective C++:通过引用捕获异常
当你写一个catch子句时,必须确定让异常通过何种方式传递到catch子句里.你可以有三个选择:与你给函数传递参数一样,通过指针(by pointer),通过传值(by value)或通过引用(by ...
- 使用 Eclipse 远程调试 Java 应用程序
Eclipse 中的远程调试特性 Eclipse 是一个图形化 Java 调试器前端.JDI 在 org.eclipse.jdt.debug 包中实现.本文不详细讨论 JDI 实现.参见 参考资料 获 ...
- docker创建自己的镜像并配置nginx
前言 最近在研究docker,记录一下如何创建一个属于自己的镜像 本次使用linux版本为centos7.4(centos6也可以使用docker,只不过有部分功能只有7才有) 本次创建的镜像为cen ...