Java for LeetCode 050 Pow(x, n)
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)的更多相关文章
- Java实现 LeetCode 50 Pow(x,n)
50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 消灭textarea中的神秘空格
之前在做页面的时候经常发现写的textarea中会有一些默认的空格出现,鼠标可以在里面任意点击.这个问题折腾了好久,后来发现,原来是<textarea></textarea>标 ...
- BIEEE 创建多维钻取分析(4)
在上一节时,我们创建了一个基于部门号的工资分类汇总. 这里就引出了一个概念:维度 专业的解释大家自行百度,这里就不班门弄斧了.从数据的使用角度看,维度可以简单的理解成“数据分类汇总的一种依据”. 按“ ...
- BZOJ-1024 生日快乐 DFS+一丝sb的数学思考
1024: [SCOI2009]生日快乐 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 1948 Solved: 1391 [Submit][Statu ...
- TCP/IP详解 笔记九
广播和多播 多播和广播只能用于UDP包,TCP明确在两个进程间建立连接. 多播:帧只传送给属于多播组的多个接口 主机对帧的过滤过程: 通常网卡只接收那些目的地址为本物理接口地址或广播地址的帧:设置为混 ...
- 迪杰斯特拉(Java)
public class Dijsktra { public static void main(String[] args) { Dijsktra d=new Dijsktra(); int[][] ...
- Codeforce#331 (Div. 2) A. Wilbur and Swimming Pool(谨以此题来纪念我的愚蠢)
C time limit per test 1 second memory limit per test 256 megabytes input standard input output stand ...
- Entity Framework 学习总结之一:ADO.NET 实体框架概述
http://www.cnblogs.com/xlovey/archive/2011/01/03/1924800.html ADO.NET 实体框架概述 新版本中的 ADO.NET 以新实体框架为特色 ...
- MVC区域 视图必须派生自 WebViewPage 或 WebViewPage<TModel>
http://blog.csdn.net/iack_ji/article/details/16965885 今天在学习 mvc区域时,将区域控制器类 外迁到其他的程序集的练习中出现了"视图必 ...
- mysql prepare语句使用
语法 PREPARE statement_name FROM sql_text /*定义*/ EXECUTE statement_name [USING variable [,variable...] ...
- Spring学习5-Spring整合JDBC及其事务处理(注解方式)
一.整合的步骤 1.步骤一:首先要获得DataSource连接池(推荐使用B方式): 要对数据库执行任何的JDBC操作,需要有一个Connection.在Spring中,Connection对象是 ...