Pow(x,n) leetcode java
题目:
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的更多相关文章
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Pow(x, n) leetcode
Implement pow(x, n). Subscribe to see which companies asked this question 利用依次消去二进制位上的1,来进行计算 double ...
- 【目录】LeetCode Java实现
这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
随机推荐
- 【基础知识】Dom基础
[学习日记]Dom基础 1. 内容:使用JavaScript操作Dom进行DHTML开发 2. 目标:能共使用JavaScript操作Dom实现常见的DHTML效果 3. DHTML= C ...
- bzoj 3956: Count
3956: Count Description Input Output Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N< ...
- java集合之二(collection架构)
转载请注明出处:http://www.cnblogs.com/skywang12345/p/3308513.html 首先,我们对Collection进行说明.下面先看看Collection的一些框架 ...
- LeetCode:整数反转(Reserve Integer)
public class ReserveInteger { public int reverse(int x) { //用于接收个位数(10的余数) int remainder; //是否负数 int ...
- Windows下Python版本的切换
通常在Windows系统下我们可能安装了多个Python版本,那么该如何进行版本的切换呢?下面就Python2.7与Python3.0版本进行简单说明. 1.首先需要在Windows上安装Python ...
- macbook pro 开发帮助
java安装目录 /Library/java/JavaVirtualMachines/ 设置快捷目录 vim .bash_profile 文件中追加一下信息:export dirname=目录路径 重 ...
- Perforce-Server迁移
Author: JinDate: 20140827System: Windows 2008 R2 从Windows 2008 R2迁移到Windows 2008 R2 linux版本迁移官方文档htt ...
- [转贴] start-stop-daemon命令
转贴自 http://www.lampblog.net/ubuntu/start-stop-daemon%E5%91%BD%E4%BB%A4/ 1.功能作用 启动和停止系统守护程序 2.位置 /sbi ...
- 在C#中实现视频播放器
当我们需要在C#中实现视频播放器的时候,可以使用如下几种方法: 一.使用MediaPlayer ActiveX控件 在C#中支持视屏播放器最简单的方式就是插入MediaPlayer控件了,在WPF中还 ...
- Send custom commands to Mass Storage device
http://stackoverflow.com/questions/14363152/send-custom-commands-to-mass-storage-device I have devel ...