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因超时无 ...
随机推荐
- [转]40多个关于人脸检测/识别的API、库和软件
[转]40多个关于人脸检测/识别的API.库和软件 http://news.cnblogs.com/n/185616/ 英文原文:List of 40+ Face Detection / Recogn ...
- DSP28335的SPI发送
#include "DSP2833x_Device.h"#include "DSP2833x_Examples.h"unsigned char table[]= ...
- < java.util >-- Set接口
Set接口中的方法和Collection中方法一致的.Set接口取出方式只有一种,迭代器. |--HashSet:底层数据结构是哈希表,线程是不同步的.无序,高效: HashSet集合保证元素唯一性: ...
- DES,3DES,AES这三种对称密钥的区别与联系
DES:Data Encryption Standard(数据加密标准,又美国国密局,选中的IBM的方案,密钥长度为56,标准提出是要使用64位长的密钥,但是实际中DES算法只用了64位中的56位密钥 ...
- 团队项目——特定功能NABC
我们要做的项目是截屏软件,目前决定做电脑端的应用 我觉得这个软件应该具有随意截屏的功能,就是可以用鼠标拖动线条,只要形成闭合图形就可以将线条内的图像截取出来: NABC模型: N(Need): 许多人 ...
- 团队开发(NABC模型)
1.NEED(需求) 基于铁大目前打电话订水的现状,我们发现了一些问题,例如不能及时送到水,水源不足等问题.本来手机在我们生活中就是非常普及的,尤其是对我们大学生来说,我们似乎患上了“手机依赖症”,没 ...
- jQuery实现模拟滚动条效果;
滚动条在web开发中,很常见,原生的HTML滚动条很难看,因此很多网站借助JS来模拟实现滚动条效果: 滚动条的实现原理其实比较简单,拿垂直滚动条来说: 1),最外层容器需要设置overflow:hid ...
- Python 删除列表中的重复数据
list0=['b','c', 'd','b','c','a','a'] 方法1:使用set() list1=sorted(set(list0),key=list0.index) # sorted o ...
- submit和button的区别
两者主要区别在于:submit可以提交表单(form),而button如果不指定onclick等事件处理函数,它是不做任何事情的.注意哦,在页面上<input type="submit ...
- hibernate 注解写在哪?
是写在get方法上还是 还是成员变量上? 一般 成员变量是私有的,如果写在成员变量上,那么hibernate就能过通过反射机制直接访问到私有变量,破坏了数据的封装性: 所以 :推荐写在方法上,虽然写的 ...