50. Pow(x, n)
题目:
Implement pow(x, n).
链接: http://leetcode.com/problems/powx-n/
题解:
使用二分法求实数幂,假如不建立临时变量halfPow,直接return计算结果的话会超时,还没仔细研究为什么。
Time Complexity - O(logn), Space Complexity - O(1)。
public class Solution {
public double myPow(double x, int n) {
if(x == 0)
return x;
if(n == 0)
return 1;
double halfPow = myPow(x, n / 2), result;
if(n % 2 == 0)
result = halfPow * halfPow;
else if (n > 0)
result = halfPow * halfPow * x;
else
result = halfPow * halfPow / x;
return result;
}
}
更新Update:
public class Solution {
public double myPow(double x, int n) {
if(x == 0.0)
return 0.0;
if(n == 0)
return 1.0;
if(n % 2 == 0)
return myPow(x * x, n / 2);
else {
if(n > 0)
return myPow(x * x, n / 2) * x;
else
return myPow(x * x, n / 2) / x;
}
}
}
二刷:
还是二分法。
Java:
使用临时变量:
Time Complexity - O(logn), Space Complexity - O(1)。
public class Solution {
public double myPow(double x, int n) {
if (x == 0.0) {
return x;
}
if (n == 0) {
return 1;
}
double half = myPow(x, n / 2);
if (n % 2 == 0) {
return half * half;
} else if (n > 0) {
return half * half * x;
} else {
return half * half / x;
}
}
}
不适用临时变量,使用尾递归:
Time Complexity - O(logn), Space Complexity - O(1)。
public class Solution {
public double myPow(double x, int n) {
if (x == 0.0) {
return x;
}
if (n == 0) {
return 1;
}
if (n % 2 == 0) {
return myPow(x * x, n / 2);
} else if (n > 0) {
return myPow(x * x, n / 2) * x;
} else {
return myPow(x * x, n / 2) / x;
}
}
}
三刷:
Java:
public class Solution {
public double myPow(double x, int n) {
if (x == 0.0) return 0.0;
if (n == 0) return 1.0;
if (n % 2 == 0) return myPow(x * x, n / 2);
else if (n < 0) return myPow(x * x, n / 2) / x;
else return myPow(x * x, n / 2) * x;
}
}
测试:
Reference:
blog.csdn.net/linhuanmars/article/details/20092829
https://leetcode.com/discuss/17005/short-and-easy-to-understand-solution
https://leetcode.com/discuss/52800/5-different-choices-when-talk-with-interviewers
https://leetcode.com/discuss/12004/my-answer-using-bit-operation-c-implementation
https://leetcode.com/discuss/9459/o-logn-solution-in-java
https://leetcode.com/discuss/39143/shortest-python-guaranteed
https://leetcode.com/discuss/21272/lg-n-320ms-javasolution-9-lines
https://leetcode.com/discuss/13545/simple-iterative-lg-n-solution
https://leetcode.com/discuss/62484/iterative-java-python-short-solution-o-log-n
50. Pow(x, n)的更多相关文章
- LeetCode - 50. Pow(x, n)
50. Pow(x, n) Problem's Link ----------------------------------------------------------------------- ...
- [Leetcode][Python]50: Pow(x, n)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 50: Pow(x, n)https://leetcode.com/probl ...
- leetcode 50. Pow(x, n) 、372. Super Pow
50. Pow(x, n) 372. Super Pow https://www.cnblogs.com/grandyang/p/5651982.html https://www.jianshu.co ...
- LeetCode 50. Pow(x, n) 12
50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...
- Java实现 LeetCode 50 Pow(x,n)
50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...
- 刷题-力扣-50. Pow(x, n)
50. Pow(x, n) 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/powx-n/ 著作权归领扣网络所有.商业转载请联系官方授 ...
- [LeetCode] 50. Pow(x, n) 求x的n次方
Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...
- 50. Pow(x, n) (编程技巧)
Implement pow(x, n). double sum = 1; if (n > 0) { while ((n--) > 0) sum *= x; return sum; } el ...
- [Leetcode]50. Pow(x, n)
Implement pow(x, n). 我的做法就比较傻了.排除了所有的特殊情况(而且double一般不可以直接判断==),然后常规情况用循环来做.- -||| 直接用循环,时间复杂度就比较大.应该 ...
随机推荐
- ASP.NET MVC4学习笔记之总体概述
断断续续使用ASP.NET MVC框架也有一年多了,也算积累了一些经验,唉,一直想写一些笔记好好总结一下,人太懒不想动笔,今天终于决定开始.希望自己能坚持下去. 这篇文章大体介绍ASP.NET MVC ...
- EWOULDBLOCK = EAGAIN
#define EAGAIN 11 /* Try again */ #define EINTR 4 /* Interrupted system call */ #define EWOULDBLOCK ...
- MySQL主从修复
MySQL主从故障修复 测试库:192.168.1.2 主192.168.1.3 从 192.168.1.4 主 4又是2的从库192.168.1.5 从 有人修改了192.168.1.2和192.1 ...
- GitHub error “Failed to get HEAD”
cd /要提交的文件的文件夹下 比如要提交一个名为 demo的 程序, 那么先进入demo 的文件夹里面 然后 进行以下两步 git init (有时这个是不必要的,因为xcode 自 ...
- c语言编程之队列(链表实现)
用链表实现了队列,完成了队列的入队和出队功能. #include"stdio.h" typedef int element; typedef struct Node{ struct ...
- IIS Express start introduction and applicationHost modification
1. First you need create a web project in VS 2. When you finish your project, click start then IIS E ...
- 2109&2535: [Noi2010]Plane 航空管制 - BZOJ
Description世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上, ...
- shell script 的追踪与 debug
shell script 的追踪与 debug scripts 在运行之前,最怕的就是出现语法错误的问题了!那么我们如何 debug 呢?有没有办法不需要透过直接运行该 scripts 就可以来判断是 ...
- BZOJ 3875: [Ahoi2014]骑士游戏 dp+spfa
题目链接: 题目 3875: [Ahoi2014]骑士游戏 Time Limit: 30 Sec Memory Limit: 256 MB 问题描述 [故事背景] 长期的宅男生活中,JYY又挖掘出了一 ...
- 【块状树】BZOJ 1086: [SCOI2005]王室联邦
1086: [SCOI2005]王室联邦 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 826 Solved: ...