题目:

Implement pow(x, n).

题解:

pow(x,n)就是求x的n次方。x的N次方可以看做:x^n = x^(n/2)*x^(n/2)*x^(n%2)。所以利用递归求解,当n==1的时候,x^n=x。

当然n是可以小于0的,2^(-3) = 1/(2^3)。按照上面那个规律就可以解决了。

代码如下:

 1     public double power(double x, int n) {
 2         if (n == 0)
 3             return 1;
 4  
 5         double v = power(x, n / 2);
 6  
 7         if (n % 2 == 0) {
 8             return v * v;
 9         } else {
             return v * v * x;
         }
     }
  
     public double pow(double x, int n) {
         if (n < 0) {
             return 1 / power(x, -n);
         } else {
             return power(x, n);
         }
     }

Reference: http://fisherlei.blogspot.com/2012/12/leetcode-powx-n.html

Pow(x,n) leetcode java的更多相关文章

  1. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  3. Sqrt(int x) leetcode java

    Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735  题目: Implement int sqrt(int x). Co ...

  4. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  5. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  6. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. Pow(x, n) leetcode

    Implement pow(x, n). Subscribe to see which companies asked this question 利用依次消去二进制位上的1,来进行计算 double ...

  8. 【目录】LeetCode Java实现

    这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...

  9. Single Number II leetcode java

    问题描述: Given an array of integers, every element appears three times except for one. Find that single ...

随机推荐

  1. 分布式系统的一致性算法------《Designing Data-Intensive Applications》读书笔记13

    一致性算法是分布式系统中最重要的问题之一.表面上看,这似乎很简单,只是让几个节点在某些方面达成一致.在本篇之中,会带大家完整的梳理分布式系统之中的共识算法,来更加深刻的理解分布式系统的设计. 1.原子 ...

  2. CTF Writeup 一个专门收集WP的网站

    www.ctfwp.com 创建于2019-04-15 致力于收集网上公开writeup,方便大家学习.

  3. python3.6.5中pip3无法使用

    1.在python命令行窗口中: python3 -m ensurepip 创建出pip3.exe.2.再在python3.6的安装目录下的Scripts路径下命令行 pip3 install XXX ...

  4. POJ - 1329 Circle Through Three Points 求圆

    Circle Through Three Points Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4112   Acce ...

  5. ubuntu 16.04.1 LTS postgresql安装配置

    postgresql安装--------------------二进制安装:wget https://get.enterprisedb.com/postgresql/postgresql-9.5.6- ...

  6. 递归与分治策略之循环赛日程表Java实现

    递归与分治策略之循环赛日程表 一.问题描述 设有n=2^k个运动员要进行网球循环赛.现要设计一个满足以下要求的比赛日程表: (1)每个选手必须与其他n-1个选手各赛一次: (2)每个选手一天只能参赛一 ...

  7. Qt Quick快速入门之qml布局

    Qml里面布局主要有两种,锚点布局.Grid布局. 锚点布局使用anchors附件属性将一个元素的边定位到另一个元素的边,从而确定元素的位置和大小.下面是示例 import QtQuick 2.3 i ...

  8. python IDLE 自动提示功能

    \Python27\Lib\idlelib\目录下 config-extensions.def文件修改等待时间 [AutoComplete] enable=1 popupwait=2000(2000表 ...

  9. 如何使用git工具向github提交代码

    大致分为以下几个步骤 安装git环境,工具使用msysgit github上的账号 首先在github上点击头像旁边的加号 add new ,选择new Repository,自己创建一个名字,假设取 ...

  10. 盘点Linux内核源码中使用宏定义的若干技巧(1)

    http://blog.chinaunix.net/uid-23769728-id-3141515.html