lintcode:Pow(x, n)
Pow(x, n)
Implement pow(x, n).
解题
直接顺序求解,时间复杂度O(N)
public class Solution {
/**
* @param x the base number
* @param n the power number
* @return the result
*/
public double myPow(double x, int n) {
// Write your code here
if(x == 0)
return 0;
if(n == 0)
return 1.0;
if( n<0)
return 1.0/(myPow(x,-n));
double res = x;
while(n>1){
res*=x;
n--;
}
return res;
}
}
Java Code
class Solution:
# @param {double} x the base number
# @param {int} n the power number
# @return {double} the result
def myPow(self, x, n):
# Write your code here
return x**n
递归方式
对n时候奇数还是偶数的时候进行讨论
public class Solution {
/**
* @param x the base number
* @param n the power number
* @return the result
*/
public double myPow(double x, int n) {
// Write your code here
if(x == 0)
return 0;
if(n == 0)
return 1.0;
if( n<0)
return 1.0/(myPow(x,-n));
double res = myPow(x,n/2);
if(n%2==1){
res = res * res *x;
}else{
res = res * res;
}
return res;
}
}
递归程序中间需要比较多的栈,容易发生内存溢出的情况
改写成循环的形式,效果更好,参考于libsvm源码
public class Solution {
public double myPow(double x, int n) {
if(n==0)
return 1.0;
if(x==1)
return 1.0;
if(n<0){
if (n == Integer.MIN_VALUE)
return myPow(x, n+1)/x;
else
return 1.0/myPow(x, -n);
}
double tmp = x;
double ret = 1.0;
for(int time = n;time>0;time/=2){
if(time%2==1)
ret *= tmp;
tmp*=tmp;
}
return ret;
}
}
上面程序对n是最小值得时候进行了处理,LeetCode测试样例有 2 ,Integer.MIN_VALUE ,上面单独处理,可以通过
lintcode:Pow(x, n)的更多相关文章
- [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 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- Lintcode: Fast Power 解题报告
Fast Power 原题链接:http://lintcode.com/en/problem/fast-power/# Calculate the an % b where a, b and n ar ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- 【探索】无形验证码 —— PoW 算力验证
先来思考一个问题:如何写一个能消耗对方时间的程序? 消耗时间还不简单,休眠一下就可以了: Sleep(1000) 这确实消耗了时间,但并没有消耗 CPU.如果对方开了变速齿轮,这瞬间就能完成. 不过要 ...
- [LeetCode] Super Pow 超级次方
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...
- [LeetCode] Pow(x, n) 求x的n次方
Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...
随机推荐
- .Net开源数据库设计工具Mr.E For Linq (EF 6.1) 教程(二)级联删除和触发器
1.建立级联删除 Mr.E的级联删除并非数据库自带那个级联删除,而是Mr.E自带的,所以它能触发你C#里面编写的触发器. 首先,建立级联删除关系,如下图有两个表,UserInfo和UserDocume ...
- bootstrap bootstrapTable 隐藏列
主要代码: <script type="text/javascript"> $(function () { LoadingDataListOrderRealItems( ...
- P3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队
太水了,背包DP. (转载请注明出处:http://www.cnblogs.com/Kalenda/) ; var n,f,i,j,ans,t,tt:longint; q:array[..] of l ...
- mysql启动问题access denied for user 'root'@'localhost'(using password:YES)
安装Mysql后利用SQLyogEnt启动是提示“access denied for user 'root'@'localhost'(using password:YES)”,开始我还为是因为是密码问 ...
- Netsharp产品标识自定义设置:产品名称、版权、LOGO等
阅读本文请先阅读Netsharp下载及环境搭建 Netsharp本身是一个业务基础平台,Netsharp本身基础上开发的业务产品对客户才有价值,客户看到的产品应该不是Netsharp而是具体的业务产品 ...
- cygwin and its host machine
Senario 本来我是想要修改下 machine name 在Ubuntu中的步骤是这样的 1 sudo hostname newMechineName 2 sudo vi /etc/hostnam ...
- BitMap排序
问题描述: BitMap排序思想: 用1bit位标记某个元素对应的值 优点: 效率高,不允许进行比较和移位 ...
- Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8 (转)
Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8http://www.crifan.com/android_emulator_ ...
- HDU1056 HangOver
HangOver Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descript ...
- javascript设计模式--中介者模式(Mediator)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...