Implement pow(x, n).

解题思路:

直接使用乘法实现即可,注意下,如果n很大的话,递归次数会太多,因此在n=10和n=-10的地方设置一个检查点,JAVA实现如下:

static public double myPow(double x, int n) {
if(n==1)
return x;
else if(n>1&&n<=10)
return myPow(x,n-1)*x;
if(n>10)
return myPow(myPow(x,10),n/10)*myPow(x,n%10);
else if(n==-1)
return 1/x;
else if(n<-1&&n>=-10)
return myPow(x,n+1)*(1/x);
else if(n<-10)
return myPow(myPow(x,10),n/10)*myPow(x,n%10);
else return 1;
}

Java for LeetCode 050 Pow(x, n)的更多相关文章

  1. Java实现 LeetCode 50 Pow(x,n)

    50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. Java编程思想学习(五) 复用类

    1.继承与组合 复用类的方法有两种:继承与组合.继承就不多说了,组合就是直接在类中new一个对象. 数组也是对象,使用数组也是组合的一种. 2.初始化基类 当创建一个导出类的对象时,该对象包含一个基类 ...

  2. bzoj 1791 DP

    首先对于一棵树我们可以tree_dp来解决这个问题,那么对于环上每个点为根的树我们可以求出这个树的一端为根的最长链,并且在tree_dp的过程中更新答案.那么我们对于环,从某个点断开,破环为链,然后再 ...

  3. 和声搜索算法-python实现

    HSIndividual.py import numpy as np import ObjFunction class HSIndividual: ''' individual of harmony ...

  4. cogs896 圈奶牛

    描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. PROGRAM NAM ...

  5. Recon-Erlang线上系统诊断工具

    Erlang系统素以稳定可靠闻名,但是它也是c实现的,也是要管理比如内存,锁等等复杂的事情,也会出现Crash,而且crash的时候大部分原因是因为内存问题.为此erlang运行期提供了强大的自省机制 ...

  6. thinkphp 3.2 单入口 多模块 不能加载index控制器问题

    菜鸟一个,大神不用看,很喜欢单入口 多模块的方式,所以想自己设置下,结果看很多教程没看懂,也看到有人在问这个问题,分享下我的项目名称是app,首先运行官方的index.php文件,app目录下生成了三 ...

  7. Config The Image URL Solution

    During the project, in order to make a unified management for the image URL , at present we make use ...

  8. 在xib里,拖一个UIView到UITableView中作为tableHeaderView

    原贴地址:http://blog.csdn.net/haoxinqingb/article/details/41683881 内容 在xib里,拖一个UIView到UITableView中作为tabl ...

  9. 使用SqlSessionTemplate实现数据库的操作

    EmployeeMapper.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE map ...

  10. 下载安装resin-3.X服务器并配置到myeclipse

    前提是先安装jdk,具体自己安装. 1.到resin官网http://www.caucho.com/download/下载相应压缩包,比如resin-3.2.0.zip 2.解压下载的resin-3. ...