428. Pow(x, n)【medium】
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.
Pow(2.1, 3) = 9.261
Pow(0, 1) = 0
Pow(1, 0) = 1
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】的更多相关文章
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- 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 ...
- 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 ...
随机推荐
- wepy - 与原生有什么不同(watcher监听器.)
<style> </style> <template> <view>监听值:{{num}}</view> </template> ...
- php之快速入门学习-11(数组排序)
PHP 数组排序 数组中的元素可以按字母或数字顺序进行降序或升序排列. PHP - 数组排序函数 在本章中,我们将一一介绍下列 PHP 数组排序函数: sort() - 对数组进行升序排列 rsort ...
- php之phpstorm入门
入门篇!见这章
- Showing a tooltip
We can provide a balloon help for any of our widgets. #!/usr/bin/python # -*- coding: utf-8 -*- &quo ...
- 1z0-052 q209_7
7: In which of the scenarios will the DBA perform recovery? (Choose all that apply.) A.The alert log ...
- html 语义化标签拾遗
1.del和ins标签 兼容性:浏览器全部支持 del:定义文档中已被删除的文本. ins:定义已经被插入文档中的文本. <!DOCTYPE html> <html lang=&qu ...
- 机器学习笔记(十)EM算法及实践(以混合高斯模型(GMM)为例来次完整的EM)
今天要来讨论的是EM算法.第一眼看到EM我就想到了我大枫哥,EM Master,千里马.RUA!!!不知道看这个博客的人有没有懂这个梗的. 好的,言归正传.今天要讲的EM算法,全称是Expectati ...
- [Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析
转自:http://blog.csdn.net/pleasecallmewhy/article/details/8934726 百度贴吧的爬虫制作和糗百的爬虫制作原理基本相同,都是通过查看源码扣出关键 ...
- glibc的几个有用的处理二进制位的内置函数(转)
— Built-in Function: int __builtin_ffs (unsigned int x) Returns one plus the index of the least sign ...
- oracle 批量更新表字段
(一) 将数字替换成汉字 第一步,去重查询 使用distinct关键字先对该字段值进行去重查询,看共有几种情况 --查询指定区间内表停诊字段的值 SELECT DISTINCT T.CLOSE_T ...