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. android一个下拉放大库bug的解决过程及思考

    android一个下拉放大库bug的解决过程及思考 起因 项目中要做一个下拉缩放图片的效果,搜索了下github上面,找到了两个方案. https://github.com/Frank-Zhu/Pul ...

  2. poj 2739 Sum of Consecutive Prime Numbers 尺取法

    Time Limit: 1000MS   Memory Limit: 65536K Description Some positive integers can be represented by a ...

  3. springMVC补充——springMVC的表单标签

    同其他标签一样,使用标签的第一步是进行标签库的导入(如果页面频繁导入比较多的标签库,应当专门建立导入标签库的页面,详见ztree的随笔) 导入标签库: <%@ taglib prefix=&qu ...

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

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

  5. Firefox+Burpsuite抓包配置(可抓取https)

    0x00 以前一直用的是火狐的autoproxy代理插件配合burpsuite抓包 但是最近经常碰到开了代理却抓不到包的情况 就换了Chrome的SwitchyOmega插件抓包 但是火狐不能抓包的问 ...

  6. tkinter菜单图标,工具栏

    所用的图片: import tkinter as tk from tkinter import messagebox, filedialog, simpledialog, colorchooser f ...

  7. MySQLdb in Python: “Can't connect to MySQL server on 'localhost'”

    因为我使用的是win64,所以在此系统下,需要设置为 127.0.0.1 #coding=utf-8 import MySQLdb if __name__ == '__main__': # 打开数据库 ...

  8. CentOS 6.5关闭防火墙

    关闭命令:  service iptables stop 永久关闭防火墙:chkconfig iptables off 两个命令同时运行,运行完成后查看防火墙关闭状态 service iptables ...

  9. android 图片二维码识别和保存(一)

    最新业务开发二维码识别的功能,这个功能,在很多应用上都有,比如微信长按图片识别二维码,如果图片中存在可以识别的二维码时,可以增加一个选项 识别二维码.那么如何去实现这个功能呢.这里其实也非常简单,首先 ...

  10. selenium自动化之处理浏览器警告弹窗

    有的网站会弹出类似如下图的警告弹窗,你会发现这种弹窗在html源码中怎么也定位不到,接下来将介绍这种弹窗的处理方式. 其实这种弹窗是不属于html的元素的,他是属于浏览器自带的弹窗,所以用定位元素的方 ...