LeetCode第[50]题(Java):Pow(x, n)
题目:求x的n次幂
难度:Medium
题目内容:
Implement pow(x, n), which calculates x raised to the power n (xn).
翻译:
实现计算x的n次幂。
Example 1:
Input: 2.00000, 10
Output: 1024.00000
Example 2:
Input: 2.10000, 3
Output: 9.26100
Example 3:
Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
Note:
- -100.0 < x < 100.0
- n is a 32-bit signed integer, within the range [−231, 231 − 1]
我的思路:呃,没啥好思路,只会硬刚
1、幂等于0,则直接返回1,负幂则转为正然后将x变为1/x;
2、循环相乘。。。
我的代码:
public double myPow(double x, int n) {
if(n == 0)
return 1;
if(n<0){
n = -n;
x = 1/x;
} double ans = x;
while (n-- > 1) {
ans *= x;
} return ans;
}
我的复杂度:O(n)
结果——291 / 304 test cases passed. Time Limit Exceeded(超时)
Last executed input:
0.00001 2147483647
内心os:看来O(n)的方式不行。得用O(logn)?那么就是递归?这个用递归也是O(n)啊。。。
编码过程中的问题:
1、一开始直接使用的是x*=x,发现通过率比较低,并且报错的结果很大,才发现这样不是求幂而是求开多少次平方。。。
答案代码:
public double myPow(double x, int n) { if(n == 0)
return 1;
if (n == Integer.MIN_VALUE) {
return 1/x*myPow(x, n+1);
}
if(n<0){
n = -n;
x = 1/x;
} return (n%2 == 0) ? myPow(x*x, n/2) : x*myPow(x*x, n/2);
}
答案复杂度:O(logN)
答案思路:果然是利用递归。。。
前面和我一样的,不过在最后递归的时候做了判断:
如果n是偶数,那么就取(x2)(n/2)
如果n是奇数,则取 x*(x2)(n/2)
这段代码干什么用的?
if (n == Integer.MIN_VALUE) {
return 1/x*myPow(x, n+1);
}
如果n为-231,那么此时取反的话由于n是int则不能表示成231,
所以有两种解决办法:
1、和我一样,直接写个判断把一个x提出来再继续算。
2、直接将整个方法提取出来,另外写一个递归方法,此方法的参数写成(double x, long n)就不存在越界的情况了。
LeetCode第[50]题(Java):Pow(x, n)的更多相关文章
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode(50):Pow(x, n)
Medium! 题目描述: 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- LeetCode第[15]题(Java):3Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c ...
随机推荐
- 《挑战程序设计竞赛》2.6 数学问题-辗转相除法 AOJ0005 POJ2429 1930(1)
AOJ0005 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0005 题意 给定两个数,求其最大公约数GCD以及最小公倍数LCM. ...
- Spoken English Practice(appetite,ache,meds,change, stop)Look like you got your appetite back, that's a good sign.
绿色:连读: 红色:略读: 蓝色:浊化: 橙色:弱读 下划线_为浊化 口语蜕变(2017/6/27) ...
- ZooKeeper 基本介绍
Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储, Zookeeper 作用主要是用来维护和监控存储的数 ...
- cocopods
一.什么是CocoaPods 1.为什么需要CocoaPods 在进行iOS开发的时候,总免不了使用第三方的开源库,比如SBJson.AFNetworking.Reachability等等.使用这些库 ...
- 关于vtt 与 srt 字幕 的相互转换
我在下载的udacity中教程时,字幕和视频是分离的,对于英文还无法完全听懂的我来说,字幕还是比较重要.不想看解释的可直接跳到最后复制代码运行即可. 查看了vtt和srt的区别,使用记事本打开vtt和 ...
- django一些配置与ORM
- Linux 使用crontab定时备份Mysql数据库
项目中数据库的数据是至关重要的!在实际项目中,遇到有客户机房断电导致数据库数据丢失的问题,又因为备份容灾不及时,导致部分数据恢复不了,而刚好这部分丢失的数据对于客户来说又是至关重要的,那么怎么办呢?盲 ...
- TensorFlow学习笔记(二)深层神经网络
一.深度学习与深层神经网络 深层神经网络是实现“多层非线性变换”的一种方法. 深层神经网络有两个非常重要的特性:深层和非线性. 1.1线性模型的局限性 线性模型:y =wx+b 线性模型的最大特点就是 ...
- Tachyon架构剖析--王家林老师
- Python高阶函数-闭包
高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 在这里我们首先回忆一下python代码运行的时候遇到函数是怎么做的. 从python解释器开始执行之后,就在内存中开辟了一个空间 每当 ...