leetcoder-50-Pow(x, n)
Pow(x, n)
能够直接用库函数pow(x,n)一步搞定,但明显这样就没意思了。
class Solution {
public:
double myPow(double x, int n) {
if(n<0) return 1.0/myPow_1(x,-n);
else return myPow_1(x,n);
}
double myPow_1(double x,int n)
{
if(n==0) return 1.0;
double y=myPow_1(x,n/2); // 不能用n>>1 T_T 不知道什么原因
if(n&1) return y*y*x;
else return y*y;
}
};
public:
double myPow(double x, int n) {
if(n<0) return 1.0/myPow_1(x,-n);
else return myPow_1(x,n);
}
double myPow_1(double x,int n)
{
if(n==0) return 1.0;
double y=myPow_1(x,n/2); // 不能用n>>1 T_T 不知道什么原因
if(n&1) return y*y*x;
else return y*y;
}
};
位运算
class Solution {
public:
double myPow(double x, int n) {
if(n<0){
n=-n;
x=1.0/x;
}
double ans=1;
while(n){
if(n&1) ans=ans*x;
x=x*x;
n=n/2;
}
return ans;
}
};
public:
double myPow(double x, int n) {
if(n<0){
n=-n;
x=1.0/x;
} double ans=1;
while(n){
if(n&1) ans=ans*x;
x=x*x;
n=n/2;
}
return ans;
}
};
leetcoder-50-Pow(x, n)的更多相关文章
- LeetCode - 50. Pow(x, n)
50. Pow(x, n) Problem's Link ----------------------------------------------------------------------- ...
- [Leetcode][Python]50: Pow(x, n)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 50: Pow(x, n)https://leetcode.com/probl ...
- leetcode 50. Pow(x, n) 、372. Super Pow
50. Pow(x, n) 372. Super Pow https://www.cnblogs.com/grandyang/p/5651982.html https://www.jianshu.co ...
- LeetCode 50. Pow(x, n) 12
50. Pow(x, n) 题目描述 实现 pow(x, n),即计算 x 的 n 次幂函数. 每日一算法2019/5/15Day 12LeetCode50. Pow(x, n) 示例 1: 输入: ...
- Java实现 LeetCode 50 Pow(x,n)
50. Pow(x, n) 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10 输出: 1024.00000 示例 2: 输入: 2.10000, ...
- 刷题-力扣-50. Pow(x, n)
50. Pow(x, n) 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/powx-n/ 著作权归领扣网络所有.商业转载请联系官方授 ...
- [LeetCode] 50. Pow(x, n) 求x的n次方
Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...
- 50. Pow(x, n) (编程技巧)
Implement pow(x, n). double sum = 1; if (n > 0) { while ((n--) > 0) sum *= x; return sum; } el ...
- 50. Pow(x, n)
题目: Implement pow(x, n). 链接: http://leetcode.com/problems/powx-n/ 题解: 使用二分法求实数幂,假如不建立临时变量halfPow,直接r ...
- [Leetcode]50. Pow(x, n)
Implement pow(x, n). 我的做法就比较傻了.排除了所有的特殊情况(而且double一般不可以直接判断==),然后常规情况用循环来做.- -||| 直接用循环,时间复杂度就比较大.应该 ...
随机推荐
- 21:序列化django对象
django的序列化框架提供了一个把django对象转换成其他格式的机制,通常这些其他的格式都是基于文本的并且用于通过一个管道发送django对象,但一个序列器是可能处理任何一个格式的(基于文本或者不 ...
- JQuery 实现 锚点跳转
$('.nav-jump').click(function() { $('html,body').animate( { scrollTop:$($.attr(this, 'href')).offset ...
- AC日记——小魔女帕琪 洛谷 P3802
小魔女帕琪 思路: 概率公式计算: 代码: #include <bits/stdc++.h> using namespace std; ],sig; int main() { ;i< ...
- [水煮 ASP.NET Web API2 方法论](12-4)OData 支持的 Function 和 Action
问题 在 Web API 中使用 OData Function 和 Action. 解决方案 可以通过 ODataModelBuilder,使用 OData 构建 ASP.NET Web API, E ...
- 转:LLVM与Clang的概述及关系
转:http://www.cnblogs.com/saintlas/p/5738739.html LLVM是构架编译器(compiler)的框架系统,以C++编写而成,用于优化以任意程序语言 ...
- Codeforces Round #277 (Div. 2) D. Valid Sets (DP DFS 思维)
D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- [NOIP 2004] T3 合并果子
居然和BZOJ 1724完全一样o(╯□╰)o #include <bits/stdc++.h> using namespace std; typedef long long ll; in ...
- AC自动机及KMP练习
好久都没敲过KMP和AC自动机了.以前只会敲个kuangbin牌板子套题.现在重新写了自己的板子加深了印象.并且刷了一些题来增加自己的理解. KMP网上教程很多,但我的建议还是先看AC自动机(Trie ...
- BZOJ 1430 小猴打架(prufer编码)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1430 [题目大意] 一开始森林里面有N只互不相识的小猴子,它们经常打架, 但打架的双方 ...
- 【欧拉函数】BZOJ2818-GCD
怎么漏了这一道……本来想要水一水,结果忘记了φ[1]=1,果然要滚一遍前面的知识…… #include<iostream> #include<cstdio> #include& ...