题目:求x的n次幂

难度:Medium

题目内容

Implement pow(xn), 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)的更多相关文章

  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 + ...

  2. 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 ...

  3. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  4. 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 ...

  5. LeetCode(50):Pow(x, n)

    Medium! 题目描述: 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...

  6. 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 ...

  7. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

  8. 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 ...

  9. 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  ...

随机推荐

  1. 与Mysqli相关的四种数据库取值

    <!--取值方案一:通过数字数组 fetch_row()--><meta http-equiv="Content-Type" content="text ...

  2. Powershell&.NET数值取整处理

    如何取一个数的整数值? 使用类型强制转换 Powershell的强制转换有2种方式,一种是直接类型强制转换,另一种是通过-as运算符进行转换 PS F:\> [int] (3 / 2) # 直接 ...

  3. MyBatis 映射文件详解

    1. MyBatis 映射文件之<select>标签 <select>用来定义查询操作; "id": 唯一标识符,需要和接口中的方法名一致; paramet ...

  4. 阻塞IO 非阻塞IO 异步IO

    阻塞IO 一般表现为 进程/线程 调用IO操作后就一直死循环等待,直至IO操作结束,返回IO结果 非阻塞IO 一般表现为 进程/线程 调用IO操作后,可以先去干别的事情,但是每隔一段时间,回去询问一下 ...

  5. python 作用域知识点整理

    1.块级作用域 想想此时运行下面的程序会有输出吗?执行会成功吗? 1 2 3 4 5 6 7 8 9 10 11 12 #块级作用域   if 1 == 1:     name = "lzl ...

  6. linux c编程:互斥锁

    们常说互斥锁保护临界区,实际上是说保护临界区中被多个线程或进程共享的数据.互斥锁保证任何时刻只有一个线程在执行其中的代码. 互斥锁具有以下特点: ·原子性:把一个互斥锁定义为一个原子操作,这意味着操作 ...

  7. excel数据生成sql insert语句

    excel表格中有A.B.C三列数据,希望导入到数据库users表中,对应的字段分别是name,sex,age . 在你的excel表格中增加一列,利用excel的公式自动生成sql语句,方法如下: ...

  8. python 多进程使用Queue通信的例子

    import time from multiprocessing import Process,Queue MSG_QUEUE = Queue(5) def startA(msgQueue): whi ...

  9. 如何在浏览器网页中显示word文件内容

    如何在浏览器网页中显示word文件内容 把word文件读到byte[]中,再Response.OutputStream.Write(bytes)到客户端去 Page_Load事件中写: //FileS ...

  10. 微信小程序学习笔记(2)--------框架之目录结构

    框架提供了自己的视图层描述语言 wxml 和 WXSS,以及基于 JavaScript 的逻辑层框架,并在视图层与逻辑层间提供了数据传输和事件系统. 一.响应的数据绑定 框架的核心是一个响应的数据绑定 ...