Pow(x, n)——这也能用二分法!!!!
Implement pow(x, n).
下面介绍一下解决该问题的几种方法以及要注意的地方:
1)最直观容易想到的方法就是用递归方法求n个x的乘积,注意考虑n的正负号,时间复杂度为O(n)
class Solution {
public:
double myPow(double x, int n) {
if(n==)
return 1.0;
if(n<)
return 1.0/pow(x,-n);
return x*pow(x,n-);
}
};
2)考虑到n个x相乘式子的对称关系,可以对上述方法进行改进,从而得到一种时间复杂度为O(logn)的方法,递归关系可以表示为pow(x,n) = pow(x,n/2)*pow(x,n-n/2)
class Solution {
public:
double myPow(double x, int n) {
if(n==)
return 1.0;
if(n<)
return 1.0/pow(x,-n);
double half = pow(x,n>>);
if(n%==)
return half*half;
else
return half*half*x;
}
};
Pow(x, n)——这也能用二分法!!!!的更多相关文章
- leetcode 二分法 Pow(x, n)
Pow(x, n) Total Accepted: 25273 Total Submissions: 97470My Submissions Implement pow(x, n). 题意:求x的n次 ...
- [LeetCode] Pow(x, n) (二分法)
Implement pow(x, n). 刚开始没想到,后来看remlost的博客才写出来,代码很简练: class Solution { public: double pow(double x, i ...
- 50. Pow(x, n)
题目: Implement pow(x, n). 链接: http://leetcode.com/problems/powx-n/ 题解: 使用二分法求实数幂,假如不建立临时变量halfPow,直接r ...
- 二分法习题HDU2199
AC代码 : #include<iostream>#include<cmath>using namespace std;double y;double f(double n){ ...
- [Leetcode]50. Pow(x, n)
Implement pow(x, n). 我的做法就比较傻了.排除了所有的特殊情况(而且double一般不可以直接判断==),然后常规情况用循环来做.- -||| 直接用循环,时间复杂度就比较大.应该 ...
- D - WE POJ - 3273 (二分法)
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...
- PAT A1010 Radix (25 分)——进制转换,二分法
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- C语言复习---迭代法,牛顿迭代法,二分法求根
一:用迭代法求 x=√a.求平方根的迭代公式为:X(n+1)=(Xn+a/Xn) /2. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> ...
- LeetCode: Pow(x, n) 解题报告
Pow(x, n) Implement pow(x, n). SOLUTION 1: 使用二分法. 1. 负数的情况,使用以下的公式转化为求正数power,另外,考虑到MIN_VALUE可能会造成越界 ...
随机推荐
- Linux之poll与select20160619
使用非阻塞I/O的应用程序通常会使用select()和poll()系统调用查询是否可对设备进行无阻塞的访问,这两个系统调用最终又会引发设备驱动中的poll()函数被执行 如果当前不可读(先调用驱动.p ...
- Codeforces Round #345 (Div. 2) A
A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- bzoj 1568 [JSOI2008]Blue Mary开公司 超哥线段树
[JSOI2008]Blue Mary开公司 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 1808 Solved: 639[Submit][Sta ...
- opencv学习笔记二
1,读取照片(imread()) 2,处理照片(cvtcolor()) 3,命名窗口(namewindow()) 4,显示照片(imshow()) 5,保存照片(imwrite()) #include ...
- POJ2528 线段树离散化
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 62771 Accepted: 18120 ...
- Codeforces Round #398 (Div. 2) B,C
B. The Queue time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Jenkins自动化构建系列:01敏捷开发、自动化构建与持续集成
<SVN与TortoiseSVN实战系列>已写完,今天新开一个<Jenkins自动化构建系列>,上周听了Bob Jiang老师的Agile1001公开课,一直想写个总结,这篇关 ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- js for等循环 跳出多层循环
js for 循环 跳出多层循环 ,,,,,,,]; // 8个数 ,,,,,,,]; //8个数 testFor(); console.log(') function testFor() { ;k& ...
- bzoj 2956: 模积和 ——数论
Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表 ...