Implement pow(x, n).

Notice

You don't need to care about the precision of your answer, it's acceptable if the expected answer and your answer 's difference is smaller than 1e-3.

 
Example

Pow(2.1, 3) = 9.261
Pow(0, 1) = 0
Pow(1, 0) = 1
Challenge

O(logn) time

参考了@grandyang 的代码

解法一:

 class Solution {
public:
double myPow(double x, int n) {
double res = 1.0;
for (int i = n; i != ; i /= ) {
if (i % != ) {
res *= x;
}
x *= x;
}
return n < ? / res : res;
}
};

迭代

解法二:

 class Solution {
public:
double myPow(double x, int n) {
if (n < ) {
return / power(x, -n);
} return power(x, n);
}
double power(double x, int n) {
if (n == ) {
return ;
} double half = power(x, n / );
if (n % == ) {
return half * half;
} return x * half * half;
}
};

解法三:

 class Solution {
public:
/**
* @param x the base number
* @param n the power number
* @return the result
*/
double myPow(double x, int n) {
if (n == ) {
return ;
}
if (n == ) {
return x;
}
if (n == -) {
return / x;
} return myPow(x, n / ) * myPow(x, n - n / );
}
};

会超时

428. Pow(x, n)【medium】的更多相关文章

  1. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  2. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  3. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  4. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  5. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  6. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

  7. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  8. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

  9. Java for LeetCode 207 Course Schedule【Medium】

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

随机推荐

  1. PHP高级教程-高级过滤器

    PHP 高级过滤器 检测一个数字是否在一个范围内 以下实例使用了 filter_var() 函数来检测一个 INT 型的变量是否在 1 到 200 内: 实例 <?php $int = 122; ...

  2. Linux内核中网络数据包的接收-第一部分 概念和框架

    与网络数据包的发送不同,网络收包是异步的的.由于你不确定谁会在什么时候突然发一个网络包给你.因此这个网络收包逻辑事实上包括两件事:1.数据包到来后的通知2.收到通知并从数据包中获取数据这两件事发生在协 ...

  3. 动态IP或无公网IP时外网訪问内网ORACLE数据库

    ORACLE数据库是应用最多的一个数据库.一般项目应用.将ORACLE部署在内网,内网调用,及运维都仅仅能是内网完毕. 假设ORACLE主机或所在局域网没有固定公网IP,又想在外网对ORACLE进行訪 ...

  4. from VC的IDE使用技巧大全

    .cpp是c++源文件 .opt 工程关于开发环境的参数文件.如工具条位置等信息: .aps (AppStudio File),资源辅助文件,二进制格式,一般不用去管他. .clw ClassWiza ...

  5. JBoss AS 7之基本配置和部署(The Return Of The King)

    1.4 JBoss As 7基本配置 1.4.1 IP訪问控制 因默认情况下,jboss仅可通过127.0.0.1和localhost来訪问.假设你想局域网中的其他IP来訪问,你能够在standalo ...

  6. hbase中的缓存的计算与使用

    hbase中的缓存分了两层:memstore和blockcache. 其中memstore供写使用,写请求会先写入memstore,regionserver会给每个region提供一个memstore ...

  7. 启动ip wizard时报the ip wizard does not support dhcp

    启动ip wizard时报the ip wizard does not support dhcp 阅读:5502012-05-11 11:15 标签:loadrunner 打开ip wizard:开始 ...

  8. Android API之android.provider.ContactsContract.Data

    android.provider.ContactsContract.Data Constants for the data table, which contains data points tied ...

  9. php新特性:trait 关键字使用

    1.trait关键字:含义[特性] 1.1 和require include 区别: 后两者需要 实例化一个类或者静态调用,而trait相当于继承,但又不是extends关键字,它解决了单继承. 2. ...

  10. 【mysql】Innodb三大特性之insert buffer

    一.什么是insert buffer insert buffer是一种特殊的数据结构(B+ tree)并不是缓存的一部分,而是物理页,当受影响的索引页不在buffer pool时缓存 secondar ...