- 03. 数组中重复的数字
- 04. 二维数组中的查找
- 05.替换空格
- 06.从尾到头打印链表
- 07.重建二叉树
- 09.用两个栈实现队列
- 10-1.斐波那契数列
- 10-2.青蛙跳台阶问题
- 剑指 offer 11.旋转数组的最小数字
- 剑指offer 12.矩阵中的路径
- 剑指offer 13.机器人的运动范围
- 剑指offer 14-1.剪绳子
- 剑指offer 14-2.剪绳子2
- 剑指offer 15.二进制中1的个数
- 剑指offer 16.数值的整数次方
- 剑指offer 17.打印从1到最大的n位数
- 剑指offer 18.删除链表的节点
- 剑指offer 19.正则表达式匹配
- 剑指offer 20.表示数值的字符串
- 剑指offer 21.调整数组顺序使奇数位于偶数前面
- 剑指offer 22.链表中倒数第k个节点
- 剑指offer 24.反转链表
- 剑指offer 25.合并两个排序的链表
- 剑指offer 26.树的子结构
- 剑指offer 27.二叉树的镜像
- 剑指offer 28.对称的二叉树
- 剑指offer 29.顺时针矩阵
- 剑指offer 30.包含min函数的栈
- 剑指offer 31.栈的压入、弹出序列
- 剑指offer 32-1.从上到下打印二叉树
- 剑指offer 32-2.从上到下打印二叉树
- 剑指offer 32-3.从上到下打印二叉树
- 剑指offer 33.二叉搜索树的后序遍历序列
- 剑指offer 34.二叉树中和为某一值的路径
- 剑指offer 35.复杂链表的复制
- 剑指offer 36.二叉搜索树与双向链表
- 剑指offer 37.序列化二叉树
- 剑指offer 38.字符串的排列
- 剑指 offer 39. 数组中出现次数超过一半的数字
- 剑指offer 40. 最小的k个数
- 剑指offer 41.数据流中的中位数
- 剑指offer 42. 连续子数组的最大和
- 剑指offer 43. 1~n整数中1出现的次数
- 剑指offer 44. 数字序列中某一位的数字
- 剑指offer 45. 把数组排成最小的数
- 剑指offer 46.把数字翻译成字符串
- 剑指offer 47.礼物的最大值
- 剑指offer 48.最长不含重复字符的字符子串
- 剑指offer 49.丑数
- 剑指offer 50.第一个只出现一次的字符
- 剑指offer 51. 数组中的逆序对
- 剑指offer 52.两个链表的第一个公共节点
- 剑指offer 53-1. 在排序数组中查找数字
- 剑指offer 53-2. 0~n-1中缺失的数字
- 剑指offer 54. 二叉搜索树的第k大节点
- 剑指offer 55-1. 二叉树的深度
- 剑指offer 55-2. 平衡二叉树
- 剑指offer 56-1. 数组中数字出现的次数
- 剑指offer 56-2. 数字中出现的次数
- 剑指offer 57-1. 和为s的两个数字
- 剑指offer 57-2. 和为s的连续正数序列
- 剑指offer 58-1. 翻转单词顺序
- 剑指offer 58-2.左旋转字符串
- 剑指offer 59-1. 滑动窗口的最大值
- 剑指offer 59-2. 队列的最大值
- 剑指offer 60. n个骰子的点数
- 剑指offer 61. 扑克牌中的顺子
- 剑指offer 62. 圆圈中最后剩下的数字
- 剑指offer 63. 股票的最大利润
- 剑指offer 64. 求1+2+…+n
- 剑指offer 65. 不用加减乘除做加法
- 剑指offer 66. 构建乘积数组
- 剑指offer 67. 把字符串转成整数
- 剑指offer 68-1. 二叉搜索树的最近公共祖先
- 剑指offer 68-2. 二叉树的最近公共祖先
剑指offer 68-2. 二叉树的最近公共祖先
题目描述
实现 pow(x, n) ,即计算 x 的 n 次幂函数(即,x^{n})。不得使用库函数,同时不需要考虑大数问题。
示例 1:
输入:x = 2.00000, n = 10
输出:1024.00000
示例 2:
输入:x = 2.10000, n = 3
输出:9.26100
示例 3:
输入:x = 2.00000, n = -2
输出:0.25000
解释:2-2 = 1/22 = 1/4 = 0.25
提示:
- -100.0 < x < 100.0
- -2^{31} <= n <= 2^{31}-1
- -10^{4} <= x^{n} <= 10^{4}
算法
(快速幂) O(logn)
思路还是比较直接的,如果指数是负数,先不管负号,按正数来计算乘法的结果,最后再判断如果指数是负数的话,返回用 1 除以答案的结果,否则直接返回即可。
注意:直接计算乘法会超时,需要用快速幂。
时间复杂度
O(logn)
空间复杂度
O(1)
C++ 代码
写法 1
class Solution {
public:
double myPow(double x, int _n) {
if (!x || x == 1) return x;
long long n = _n;
bool is_minus = _n < 0;
n = is_minus ? -n : n;
// 快速幂
double res = 1;
while (n) {
if (n & 1) res = res * x;
x *= x;
n >>= 1;
}
return is_minus ? 1 / res : res;
}
};
写法 2
class Solution {
public:
double myPow(double x, int n) {
typedef long long LL;
double res = 1;
bool is_minus = n < 0;
// 快速幂
for (LL k = abs(LL(n)); k; k >>= 1) {
if (k & 1) res *= x;
x *= x;
}
return is_minus ? 1 / res : res;
}
};
Java 代码
写法 1
public class Solution {
public double myPow(double x, int _n) {
if (x == 0 || x == 1) {
return x;
}
long n = _n;
boolean isMinus = _n < 0;
n = isMinus ? -n : n;
// 快速幂
double res = 1;
while (n > 0) {
if ((n & 1) == 1) {
res *= x;
}
x *= x;
n >>= 1;
}
return isMinus ? 1 / res : res;
}
}
写法 2
public class Solution {
public double myPow(double x, int n) {
double res = 1;
boolean isMinus = n < 0;
// 快速幂
for (long k = Math.abs((long)n); k > 0; k >>= 1) {
if ((k & 1) == 1) {
res *= x;
}
x *= x;
}
return isMinus ? 1 / res : res;
}
}
Python 代码
写法 1
class Solution:
def myPow(self, x: float, n: int) -> float:
if x == 0 or x == 1:
return x
is_minus = n < 0
n = -n if is_minus else n
# 快速幂
res = 1
while n > 0:
if n & 1:
res *= x
x *= x
n >>= 1
return 1 / res if is_minus else res
写法 2
class Solution:
def myPow(self, x: float, n: int) -> float:
res = 1
is_minus = n < 0
# 快速幂
k = abs(n)
while k > 0:
if k & 1:
res *= x
x *= x
k >>= 1
return 1 / res if is_minus else res
本文由读者提供,Github地址:https://github.com/tonngw
点击面试手册,获取本站面试手册PDF完整版