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. C++ Standard Template Library STL(undone)

    目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...

  2. java 文件读取大全

    1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile {     /**      * 以字节为单位读取文件,常用 ...

  3. Php学习之SESSION反序列化机制

    在php.ini中存在三项配置项:session.save_path="" --设置session的存储路径session.save_handler="" -- ...

  4. POJ3267 The Cow Lexicon(DP+删词)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9041   Accepted: 4293 D ...

  5. 使用servletAPI三种方式简单示例

    一.直接实现Action接口或集成ActionSupport类(推荐) public class HelloAction implements Action { @Override public St ...

  6. 修复UIImagePickerController偷换StatusBar颜色的问题

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:( ...

  7. 锋利的jQuery-7--$.extend()

    $.extend()主要有两个功能: 1.扩展jQuery对象 jQuery.extend({ min: function(a, b) { return a < b ? a : b; }, }) ...

  8. 常用webshell提权方法总结

    pcAnywhere提权:1.利用pcAnywhere提权,前提条件是pcAnywhere默认安装到默认路径,以及它的目录安全权限有users权限,如果管理员删除了users和power users用 ...

  9. 高性能的分布式内存对象缓存系统Memcached

    Memcached概述   什么是Memcached? 先看看下面几个概念: Memory:内存存储,不言而喻,速度快,对于内存的要求高,不指出的话所缓存的内容非持久化.对于CPU要求很低,所以常常采 ...

  10. 百度文库,linux下安装oracle客户端

    linux单独安装oracle client(oracle客户端) 更新:2013-10-17 18:30 | 标签:linux oracle   1.要远程使用oracle,先下载下面三个文件,注意 ...