题目:求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. Fiddler 抓包工具使用详解

    博客园 首页 新随笔 联系 订阅 管理 随笔 - 20  文章 - 0  评论 - 64 Fiddler 抓包工具总结 阅读目录 1. Fiddler 抓包简介 1). 字段说明 2). Statis ...

  2. 查看linux目录剩余空间大小

    df命令是linux系统以磁盘分区为单位查看文件系统,可以加上参数查看磁盘剩余空间信息,命令格式: df -hl   显示格式为: 文件系统 容量 已用 可用 已用% 挂载点 Filesystem S ...

  3. 巨蟒python全栈开发-第4天 列表&元组&range

    今日内容大纲 1. 什么是列表 定义: 能装对象的对象 在python中使用[]来描述列表, 内部元素用逗号隔开. 对数据类型没有要求 列表存在索引和切片. 和字符串是一样的. 2. 相关的增删改查操 ...

  4. java容器的线程安全性

    参考:https://www.cnblogs.com/yjd_hycf_space/p/7760248.html 线程安全的: Vector HashTable StringBuffer 线程不安全的 ...

  5. Python(数据库之数据类型)

    一.创建表的完整语法 #[]内的可有可无,即创建表时字段名和类型是必须填写的,宽度与约束条件是可选择填写的. create table 表名( 字段名1 类型[(宽度) 约束条件], 字段名2 类型[ ...

  6. MySQL(多表的表记录的查询)

    一.外键约束 1.创建外键 --- 每一个班主任会对应多个学生 , 而每个学生只能对应一个班主任 ----主表 CREATE TABLE ClassCharger( id TINYINT PRIMAR ...

  7. 1.Oracle数据库查看用户锁表和对表解锁的sql语句

    ① 查看用户锁表 select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.lock ...

  8. android6.0获取通讯录权限

    android6.0中,获取通讯录的权限是    <uses-permission android:name="android.permission.GET_ACCOUNTS" ...

  9. IE调试页面总结

    随着IE版本的升级,IE变的越来越强大,随之带来的问题也是越来越明显,如:如何调试在低版本的浏览器中 的情况 IE9的方法: 出于未知需求,用户在安装了较高版本IE浏览器(IE9)之后,又需要使用低版 ...

  10. jsp验证正则表达式

    jsp验证正则表达式 下面都是我收集的一些比较常用的正则表达式,因为平常可能在表单验证的时候,用到的比较多.特发出来,让各位朋友共同使用.呵呵. 匹配中文字符的正则表达式: [u4e00-u9fa5] ...