leetcode — powx-n
/**
 * 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的更多相关文章
- [Leetcode] powx n  x的n次方
		Implement pow(x, n). 题意:计算x的次方 思路:这题的思路和sqrt的类似,向二分靠近.例如求4^5,我们可以这样求:res=4.4*4^4.就是将每次在res的基础上乘以x本身, ... 
- 我为什么要写LeetCode的博客?
		# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ... 
- LeetCode All in One 题目讲解汇总(持续更新中...)
		终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ... 
- [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 ... 
- 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 ... 
- Leetcode 笔记 112 - Path Sum
		题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ... 
- Leetcode 笔记 110 - Balanced Binary Tree
		题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ... 
- Leetcode 笔记 100 - Same Tree
		题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ... 
- Leetcode 笔记 99 - Recover Binary Search Tree
		题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ... 
- Leetcode 笔记 98 - Validate Binary Search Tree
		题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ... 
随机推荐
- Nikto主动扫描神器!!!
			Perl语言开发的开源web安全扫描器 Nikto只支持主动扫描:可扫描web服务器类型是不是最新版本(分析先版本与新版相比有哪些漏洞) 针对:1.软件版本.2.搜索存在安全隐患的文件.3.服务器配置 ... 
- vue工程按业务路由打包,页面只加载对应资源
			修改路由表:src/router/index.js import Vue from 'vue'; import Router from 'vue-router'; // 主要写法如下 const Te ... 
- sklearn svm基本使用
			SVM基本使用 SVM在解决分类问题具有良好的效果,出名的软件包有libsvm(支持多种核函数),liblinear.此外python机器学习库scikit-learn也有svm相关算法,sklear ... 
- 使用 Chrome 浏览器插件 Web Scraper 10分钟轻松实现网页数据的爬取
			web scraper 下载:Web-Scraper_v0.2.0.10 使用 Chrome 浏览器插件 Web Scraper 可以轻松实现网页数据的爬取,不写代码,鼠标操作,点哪爬哪,还不用考虑爬 ... 
- 学习Acegi应用到实际项目中(12)- Run-As认证服务
			有这样一些场合,系统用户必须以其他角色身份去操作某些资源.例如,用户A要访问资源B,而用户A拥有的角色为AUTH_USER,资源B访问的角色必须为AUTH_RUN_AS_DATE,那么此时就必须使用户 ... 
- docker之tomcat简单部署
			将apache-tomcat-8.0.36.tar.gz及jdk-7u79-linux-x64.gz拷贝到创建的tomcat8目录下 在tomcat8目录下创建Dockerfile文件 在Docker ... 
- [Java源码解析] -- String类的compareTo(String otherString)方法的源码解析
			String类下的compareTo(String otherString)方法的源码解析 一. 前言 近日研究了一下String类的一些方法, 通过查看源码, 对一些常用的方法也有了更透彻的认识, ... 
- 调用接口,发送https请求
			调用https接口有两种方式 一:是导入证书; 证书调用https请求本人还不知道,希望看见此博客的大神给我回复. 二:是创建信任管理器. 第一步:实现X509TrustManager接口,里面的方法 ... 
- 背水一战 Windows 10 (116) - 后台任务: 前台程序激活后台任务
			[源码下载] 背水一战 Windows 10 (116) - 后台任务: 前台程序激活后台任务 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 前台程序激活后台任务 示例演示后 ... 
- 【设计经验】4、SERDES关键技术总结
			一.SERDES介绍 随着大数据的兴起以及信息技术的快速发展,数据传输对总线带宽的要求越来越高,并行传输技术的发展受到了时序同步困难.信号偏移严重,抗干扰能力弱以及设计复杂度高等一系列问题的阻碍.与并 ... 
