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 ...
随机推荐
- 在网页浏览器中原生显示PDF文件
在网页中直接显示pdf格式的文件方便阅读.但是如果文件较大加载速度会很慢,另外如果客户端没有安装pdf阅读插件的话,也就看不了了. 这种方式的好处就是不需要转换,直接显示,而且在加载时(高级的浏览器, ...
- shell脚本中的几个括号总结(小括号/大括号/花括号)
from:http://www.cnblogs.com/hanyan225/archive/2011/10/06/2199652.html 是毋庸置疑的,方便了我们也迷惑了我们,比如这些杂七杂八的括号 ...
- Divisibility by Eight
把当前数删除几位然后能够整除与8 那么可得知大于3位数的推断能否整除于八的条件是(n%1000)%8==0 能够得出我们的结论:仅仅须要枚举后三位后两位后一位就可以知道是否可整除于8 #include ...
- exception java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
exception java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironmen ...
- 〖Linux〗Android NDK调用已编译好的C/C++动态连接库(so文件)
一.背景:假定已有应用程序zigbeeclient.cpp,内容如下: ... extern "C" { int getresult(int argc, char **argv); ...
- CSS3动画效果之Transform
无意中翻看博客发现这个属性,就顺便熟悉了一下,百度了一下和查看了CSS3帮助文档,特整理一下 Transform 适应于对任一DOM元素的2D或3D转换,转换效果有:旋转.拉伸.平移.倾斜等. 目前浏 ...
- Scala的运算符优先级:
运算符优先级决定术语的表达式分组.这会影响一个表达式是如何进行计算.某些运算符的优先级高于其他;例如,乘法运算符的优先级比所述加法运算符优先级更高: 例如X =7 + 3* 2;这里,x 被赋值13, ...
- iOS-APP启动页加载广告
概述 加载广告页, 展现跳过按钮实现倒计时功能, 并判断广告页面是否更新. 详细 代码下载:http://www.demodashi.com/demo/10698.html 目前市场上很多APP(如淘 ...
- Android API之android.os.Parcelable
android.os.Parcelable Interface for classes whose instances can be written to and restored from a Pa ...
- 原创:微信小程序页面跳转展示缓冲提示
1.在JS文件中,直接加上下面代码即可: //缓冲提醒 wx.showToast({ title: '加载中', icon: 'loading', duration: 400 })