Implement pow(x, n).

Example 1:

Input: 2.00000, 10  Output: 1024.00000

Example 2:

Input: 2.10000, 3   Output: 9.26100
package medium;

public class L50MyPow {
// 调用Math.pow() 函数
public double mypow(double x, int n) {
double nn = n;
return Math.pow(x, nn);
} //本题就x的n次方,如求2的4次方。可以4个2相乘,也可以先两个2相乘,之后两个4在相乘。
public double myPow(double x, int n) {
//防止n越界2147483647,用long类型的N来代替n
long N = n;
double result = 1.0;
// double类型的x 判断x是否为0
if ((x - 0.0 < -0.000000000001) && n < 0) {
return 0.0;
}
//指数小于零,求得的数是用负指数相反数求得的值的倒数。
if (N < 0) {
N = -N;
x = 1 / x;
}
double current_product = x;
for (long i = N; i > 0; i /= 2) {
if ((i % 2) == 1) {
result = result * current_product;
}
current_product = current_product * current_product;
}
return result;
} public static void main(String[] args) {
L50MyPow cc = new L50MyPow();
//测试负数的整数次方。 4.0
System.out.println(cc.myPow(-2, 2));
System.out.println("!!!!!!!!!!!!!!!!!!!");
//小数的负数次方 2.543114507074558E-5
double re = cc.myPow(34.00515, -3);
System.out.println(re);
//小数的大正整数次方次方 0.0
System.out.println(cc.myPow(0.00001, 2147483647));
}
}

【Leetcode】50. Pow(x, n)的更多相关文章

  1. 【LeetCode】50. Pow(x, n) 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 递归 迭代 日期 题目地址: https://le ...

  2. 【LeetCode】50. Pow(x, n) (3 solutions)

    Pow(x, n) Implement pow(x, n). 按照定义做的O(n)肯定是TLE的. 利用这个信息:x2n = (xn)2 有个注意点,当n为负是,直接取反是不可行的. 由于int的表示 ...

  3. 【一天一道LeetCode】#50. Pow(x, n)

    一天一道LeetCode系列 (一)题目 Implement pow(x, n). (二)解题 题目很简单,实现x的n次方. /* 需要注意一下几点: 1.n==0时,返回值为1 2.x==1时,返回 ...

  4. 【LEETCODE】50、数组分类,简单级别,题目:888,1013,896,485,448,697

    package y2019.Algorithm.array; import java.util.HashSet; import java.util.Set; /** * @ProjectName: c ...

  5. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  6. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  7. 【leetcode】688. Knight Probability in Chessboard

    题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...

  8. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  9. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

随机推荐

  1. c语言异常处理机制

    异常处理机制:setjmp()函数与longjmp()函数 C标准库提供两个特殊的函数:setjmp() 及 longjmp(),这两个函数是结构化异常的基础,正是利用这两个函数的特性来实现异常. 所 ...

  2. uva 210 - Concurrency Simulator (并行程序模拟)

    from CSDN: https://blog.csdn.net/su_cicada/article/details/87898579 例题6-1 并行程序模拟( Concurrency Simula ...

  3. mybatsi中文乱码问题

    乱码问题:待总结,这里先贴出网友的博客: http://blog.csdn.net/zht666/article/details/8955952

  4. shiro实战整合

    引入依赖(包括缓存等): <!-- SECURITY begin --> <dependency> <groupId>org.apache.shiro</gr ...

  5. wpf- DataGrid 常用属性和事件

    组件所在命名空间: System.Windows.Controls 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. Col ...

  6. [note]左偏树(可并堆)

    左偏树(可并堆)https://www.luogu.org/problemnew/show/P3377 题目描述 一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 ...

  7. [备忘]Windows Server 2008 R2部署FTP FileZilla Server防火墙设置

    有一台服务器,之前文件迁移少,现准备用FileZilla Server当FTP服务器,服务器系统是Windows Server 2008 R2,同样适用FileZilla Client连接服务器FTP ...

  8. 用Micro:bit做剪刀、石头、布游戏

    剪刀.石头.布游戏大家都玩过,今天我们用Micro:bit建一个剪刀.石头.布游戏! 第一步,起始 当你摇动它时,我们希望the micro:bit选择剪刀.石头.布.尝试创建一个on shake b ...

  9. 如何下载YouTube 60fps视频

    YouTube上面不仅支持分辨率为4K和8K的视频,同时也开启了对60fps视频的支持.60帧的视频广泛用于游戏和体育视频中,使视频看起来更加流畅和细腻.对游戏玩家来说,YouTube对60fps支持 ...

  10. 基于MapReduce的(用户、物品、内容)的协同过滤推荐算法

    1.基于用户的协同过滤推荐算法 利用相似度矩阵*评分矩阵得到推荐列表 已经推荐过的置零 2.基于物品的协同过滤推荐算法 3.基于内容的推荐 算法思想:给用户推荐和他们之前喜欢的物品在内容上相似的物品 ...