/**
* Source : https://oj.leetcode.com/problems/powx-n/
*
* Created by lverpeng on 2017/7/18.
*
* Implement pow(x, n).
*
*/
public class Pow { /**
* 实现x^n
* 考虑n为负数
* 当n为偶数的时候,计算x*x,以期让n快速收缩
*
* @param x
* @param n
* @return
*/
public double pow (int x, int n) {
boolean sign = true; // true:正数
if (n < 0) {
n = -n;
sign = false;
}
double result = 1.0;
while (n > 0) {
if ((n & 1) == 1) {
result *= x;
}
n = n >> 1;
x *= x;
} return sign ? result : 1.0 / result;
} public static void main(String[] args) {
Pow pow = new Pow();
System.out.println(pow.pow(2, 3));
System.out.println(pow.pow(2, 10));
System.out.println(pow.pow(2, -3));
}
}

leetcode — powx-n的更多相关文章

  1. [Leetcode] powx n x的n次方

    Implement pow(x, n). 题意:计算x的次方 思路:这题的思路和sqrt的类似,向二分靠近.例如求4^5,我们可以这样求:res=4.4*4^4.就是将每次在res的基础上乘以x本身, ...

  2. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  3. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  4. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  5. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  6. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  7. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  8. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  9. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  10. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. NodeJS (npm) 学习笔记

    零, npm是nodeJS的包管理器,下载nodeJS后会自动安装好npm. 一,windows下安装 nodeJS并配置(以及angular安装相关问题) 1, 下载安装 https://nodej ...

  2. Linux 目录结构详解

    Linux目录详解 Linux目录详解(RHEL5.4) 由于linux是开放源代码,各大公司和团体根据linux的核心代码做各自的操作,编程.这样就造成在根下的目录的不同.这样就造成个人不能使用他人 ...

  3. spring整合mybatis框架

    1.导入jar包 2.配置文件 a.    applicationContext.xml文件 <beans xmlns="http://www.springframework.org/ ...

  4. POJ - 3278 Catch That Cow bfs 线性

    #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...

  5. 为什么要使用CMake?

    如果你曾经维护过软件包的构建和安装过程,你将对CMake感兴趣.CMake是软件项目的一个开源生成管理器,它允许开发人员以简单的可移植文本文件格式指定生成参数.然后,CMake 使用此文件为本机开发工 ...

  6. ios uibutton加数字角标

    http://www.jianshu.com/p/0c7fae1cadac 第一种:https://github.com/mikeMTOL/UIBarButtonItem-Badge第二种:https ...

  7. UML顺序图知识点介绍(Sequence Diagram)

    消息 调用消息 调用(procedure call)消息的发送者把控制传递给消息的接收者,然后停止活动,等待消息接受者放弃会返回控制 在Rational Rose(2016版本如图所示) 异步消息 异 ...

  8. 怎么取cxgrid某一列的合计值

    怎么取cxgrid某一列的合计值   1.cxGrid1DBTableView1->optionsview->Footer 设为True 2.cxGrid1DBTableView1-> ...

  9. 背水一战 Windows 10 (93) - 选取器: FileOpenPicker, FolderPicker, FileSavePicker

    [源码下载] 背水一战 Windows 10 (93) - 选取器: FileOpenPicker, FolderPicker, FileSavePicker 作者:webabcd 介绍背水一战 Wi ...

  10. 玩转Kafka的生产者——分区器与多线程

    上篇文章学习kafka的基本安装和基础概念,本文主要是学习kafka的常用API.其中包括生产者和消费者, 多线程生产者,多线程消费者,自定义分区等,当然还包括一些避坑指南. 首发于个人网站:链接地址 ...