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. 从零开始搭建vue移动端项目到上线的步骤

    初始化项目 1.在安装了node.js的前提下,使用以下命令 npm install --g vue-cli 2.在将要构建项目的目录下 vue init webpack myproject(项目目录 ...

  2. A1037

    给两个序列,一一对应相乘,求最大和. 0不算数,输入时按正负共分为4个数组. #include<cstdio> #include<algorithm> #include< ...

  3. 第六节 Go数据结构之集合

    一.什么是集合 集合就是不同对象的无序聚集.那么链表和集合有什么关系呢?我们来变个魔术.如下图是各种颜色组成的链表: 下面我们一步步把链表变成集合. 第一步砍去链接 第二步去掉重复 第三步放到一个框里 ...

  4. 详解LeetCode 137. Single Number II

    Given an array of integers, every element appears three times except for one, which appears exactly ...

  5. ubuntu下tensorflow安装

    1,安装驱动,cuda,cudnn,参考本人上一篇博客http://www.cnblogs.com/zxg-DL/p/9023601.html 2,安装tensorflow   接下来是关于Tenso ...

  6. Flex copy and paste

    <?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx ...

  7. 20155223 2006-2007-2 《Java程序设计》第二周学习总结

    20155223 2006-2007-2 <Java程序设计>第二周学习总结 第三章内容总结 3.1 类型 正如我所预料的情况一样:Java脱胎于C语言,就一定会留有和C语言相近或相同的语 ...

  8. # 20155224 课堂实践 MyOD

    20155224 课堂实践 MyOD 要求 编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 提交测试代码和运行结果截图,加上学号水印,提交码云代 ...

  9. 20155318 《Java程序设计》实验三 (敏捷开发与XP实践)实验报告

    20155318 <Java程序设计>实验三 (敏捷开发与XP实践)实验报告 实验内容 XP基础 XP核心实践 相关工具 实验步骤 (一)敏捷开发与XP 软件工程是把系统的.有序的.可量化 ...

  10. Ubuntu 16.04 主题美化及常用软件安装

    一.主题美化 系统清理 系统更新: 安装完系统之后,需要更新一些补丁.Ctrl+Alt+T调出终端,执行一下代码: sudo apt-get update sudo apt-get upgrade 卸 ...