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因超时无 ...
随机推荐
- 浅谈dynamic的简单使用用法
今天看了博客园里面的dynamic用法,我犹豫从来没接触过,今天恶补了一下,把我对dynamic的认识分享了出来,大家一起学习. Visual C# 2010 引入了一个新类型 dynamic. 该类 ...
- [开源应用]利用HTTPHandler+resumableJs+HTML5实现拖拽上传[大]文件
前言: 大文件传输一直是技术上的一大难点.文件过大时,一些性提交所有的内容进内存是不现实的.大文件带来问题还有是否支持断点传输和多文件同时传输. 本文以resumableJs为例,介绍了如何在ASP. ...
- Qt使用QStackedWidget实现堆栈窗口
Qt使用QStackedWidget实现堆栈窗口 分类: QT2012-07-25 21:59 6997人阅读 评论(0) 收藏 举报 qtlistsignal 堆栈窗口可以根据选择项的不同显示不同的 ...
- BF算法
BF算法是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串P的第一个字符进行匹配,若相等,则继续比较S的第二个字符和P的第二个字符:若不相等,则比较S的第二个字符和P的第一个字符, ...
- 不会JS中的OOP,你也太菜了吧!(第二篇)
一.你必须知道的 1> 原型及原型链在继承中起到了关键的作用.所以你一定要理解他们.2> 不会JS中的OOP,你也太菜了吧!(第一篇) 二.继承的6种方法 1> 原型链继承 原型链继 ...
- 我给女朋友讲编程html系列(3) --html中的超链接标签-a标签 和 框架frame与框架集frameset
我们浏览网页的时候,当单击某段文字或图片时,就会打开一个新的网页,这里面就使用了超链接. 就比如下图是一个导航类网页,当你单击某个链接就会打开新的网页. 比如,我拿我的qq空间“金河访谈”举例,新建一 ...
- 也发一个自己实现的android简单文件选择器代码。支持多卡,排序
一个很简单的文件选择器对话框,支持双sd卡,当然前提是要有sd卡..并且实现了排序效果. 只有100多行的代码,基本的思路就是用listview显示目录下的所有子文件,再判断是文件还是目录. 利用Co ...
- 前端之JavaScript第一天学习(2)-JavaScript 使用
HTML 中的脚本必须位于 <script> 与 </script> 标签之间.脚本可被放置在 HTML 页面的 <body> 和 <head> 部分中 ...
- ORA-12505, TNS:listener does not currently know of SID given in connect descriptor (二)
异常及解决 在连接sqldeveloper出现的异常信息 在ORA-12505, TNS:listener does not currently know of SID given in connec ...
- 【BZOJ】【3207】花神的嘲讽计划 I
字符串Hash+可持久化线段树 好神奇的转化…… 蒟蒻一开始还去想AC自动机去了……然而由于a[i]的范围是小于等于n,怎么也想不出一个时间复杂度合理的方法 膜拜了题解0.0原来是字符串Hash! 首 ...