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 ...
随机推荐
- Cocos2d-X3.0 刨根问底(三)----- Director类源码分析
上一章我们完整的跟了一遍HelloWorld的源码,了解了Cocos2d-x的启动流程.其中Director这个类贯穿了整个Application程序,这章随小鱼一起把这个类分析透彻. 小鱼的阅读源码 ...
- Java编程思想学习(二) 操作符
1. 对象“赋值”:对一个对象进行操作时,我们真正操作的是对对象的引用.所以倘若“将一个对象赋值给另一个对象”,实际是将“引用”从一个地方复制到另一个地方.(引用于对象之间存在关联,但这种关联可以被改 ...
- sqlserver字段类型详解
抄了一篇不错的数据库类型,来自:http://www.cnblogs.com/andy_tigger/archive/2011/08/21/2147745.html bit 整型 bit数据类型是整型 ...
- 黑客帝国风格必备插件ProPowerTools
ProPowerTools 详细说明点这里
- HD2255奔小康赚大钱(最大权匹配模板)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdu 2050 折线分割平面
训练递推用题,第一次做这个题,蒙的,而且对了. #include <stdio.h> int main(void) { int c,a; scanf("%d",& ...
- Python socket编程之三:模拟数据库循环发布数据
1. f1.py # -*- coding: utf-8 -*- import socket import struct import sqlalchemy import pandas ####### ...
- 关于JS判断图片是否加载完成且获取图片宽度的方法
做web的同学们经常会碰到客户上传图片将网页内容区撑破了的情况,下面就这个问题我们一种如何使用js处理这个问题的方法,具体思路就是在js判断客户端的图片下载完毕之后适时的对该图片的宽度或者高度做一些处 ...
- 使用Redis SETNX 命令实现分布式锁
基于setnx和getset http://blog.csdn.net/lihao21/article/details/49104695 使用Redis的 SETNX 命令可以实现分布式锁,下文介绍其 ...
- C#中Delegate和Event以及它们的区别(转载)
一.Delegate委托可以理解为一个方法签名. 可以将方法作为另外一个方法的参数带入其中进行运算.在C#中我们有三种方式去创建委托,分别如下: public delegate void Print( ...