【LeetCode每天一题】Pow(x, n)(平方)
Implement pow(x, n), which calculates x raised to the power n (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]
思路: 这道题并不难,主要是情况考虑周全,例如 n为负数, x为0,1时等等问题。
使用最简单的方法就是逐个累乘, 但是这种方法会在 n值非常大时,运行时间太长,导致超时。并且效率低。
另外一种使用 这样一种公式来解决: an= an/2 + an/2 (当n为偶数时), an= (an/2 + an/2)a (当n为奇数时),这样计算效率提升。 时间复杂度为O(logn), 空间复杂度为O(1)。
class Solution(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
if x == 1.0 or x == 0.0: # 特殊情况的判断
return x if x == 1.0 else 0.0
neg_flag = False # 指数为负数的标志位
if n < :
neg_flag = True
n = abs(n) # 将负指数替换为正整数
res =
while n:
if n&: # 对于指数奇偶数的判断。
res *= x
x *= x # x = x * x
n = n >> # 指数除以2
if neg_flag:
return /res
return res
【LeetCode每天一题】Pow(x, n)(平方)的更多相关文章
- 【JavaScript】Leetcode每日一题-平方数之和
[JavaScript]Leetcode每日一题-平方数之和 [题目描述] 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 示例1: 输入:c = 5 ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- leetcode第37题--Count and Say
题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...
- 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的刷题利器(伪装到老板都无法diss你没有工作)
在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...
- LeetCode每天一题之两数之和
这个LeetCode刷题系列的博客权当是为自己记一下笔记吧.博客系列会从LeetCode的第一题开始刷,同时会从零开始学习[因为我就是零/(ㄒoㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...
- leetcode第三题
leetcode第三题: 题目: 给定一个字符串,找出不含有重复字符的最长子串的长度. 源码(使用java语言): class Solution { public int lengthOfLonges ...
- [LeetCode] 系统刷题5_Dynamic Programming
Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题 ...
- LeetCode 第 342 题(Power of Four)
LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether ...
随机推荐
- SVM 核方法
在 SVM 中引入核方法便可使得 SVM 变为非线性分类器,给定非线性可分数据集 $\left \{ (x_i,y_i)\right\}_{i=1}^N$,如下图所示,此时找不到一个分类平面来将数据分 ...
- db2命令参数with ur
查询DB2数据库,老遇到select * from XXX with ur, 好奇ur是什么作用(转) DB2中,共有四种隔离级:RS,RR,CS,UR,DB2提供了这4种不同的保护级别来隔离数据. ...
- AD 16 下绘图的几个技巧
1.绘制封装如果引脚过多怎么办,使用阵列粘贴功能 首先建立一个焊盘,然后选中,使用 ctrl + c 复制,注意复制确认的时候,鼠标一定要点击到焊盘中间. 选择阵列粘贴 条款就是你要复制多少个,增量就 ...
- BZOJ 1002 - 轮状病毒 - [基尔霍夫矩阵(待补)+高精度]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1002 Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生 ...
- pause
https://stackoverflow.com/questions/37063700/mm-pause-usage-in-gcc-on-intel?utm_medium=organic&u ...
- 约数,gcd,exgcd.
很多题都是要求出什么最大公约数或者最小公倍数什么的,也有一些题目是和约数个数有关的,所以需要总结一下. 首先最大公约数和最小公倍数怎么求呢? 当然是观察法了,对于一些很聪明的孩纸他们一般随便一看就秒出 ...
- hadoop 学习笔记
参考资料:<Hadoop 权威指南> 1 map处理完后,hadoop框架会将结果安装键进行排序,然后将排好的结果传给reduce 2 需要低延迟的应用不适合HDFS,对于低延迟应用HBa ...
- sql server创建临时表的两种写法和删除临时表
--创建.删除临时表 --第一种方式 create table #tmp(name varchar(255),id int) --第二种方式 select count(id) as storyNum ...
- oracle创建表空间 授权
--创建表空间 临时表空间 create temporary tablespace xiaodai_temp tempfile '/main/app/oracle/oradata/devdb/xiao ...
- es中的停用词
停用词主要是为了提升性能与精度.从早期的信息检索到如今,我们已习惯于磁盘空间和内存被限制为很小一部分,所以 必须使你的索引尽可能小. 每个字节都意味着巨大的性能提升. 词干提取的重要性不仅是因为它让搜 ...