LeetCode 50. Pow(x, n)(求幂)
来源:互联网 发布:邀请函设计软件 编辑:程序博客网 时间:2024/04/29 20:16
原题网址:https://leetcode.com/problems/powx-n/
Implement pow(x, n).
方法一:倍乘,迭代。
public class Solution { public double myPow(double x, int n) { long ln = (long)n; if (ln < 0) { x = 1.0 / x; ln = -ln; } double pow = 1.0; while (ln > 0) { double p = x; long m = 1; for(; m*2<ln; m*=2, p*=p); pow *= p; ln -= m; } return pow; }}
方法二:二分,递归。
public class Solution { public double myPow(double x, int n) { if (n==0) return 1; if (n<0) { n=-n; x=1/x; } if (n==1) return x; if (n==2) return x*x; return myPow(myPow(x, n/2), 2) * myPow(x, n-n/2*2); }}
0 0
- LeetCode 50. Pow(x, n)(求幂)
- LeetCode 50. Pow(x, n),求幂算法
- LeetCode OJ 之 Pow(x, n) (求x^n)
- [LeetCode]50. Pow(x, n)&&幂函数
- LeetCode 50. Pow(x, n)--幂实现
- LeetCode - Pow(x, n) (快速幂)
- [LeetCode]50.Pow(x, n)
- LeetCode --- 50. Pow(x, n)
- [Leetcode] 50. Pow(x, n)
- [leetcode] 50.Pow(x, n)
- LeetCode 50. Pow(x, n)
- 50. Pow(x, n) LeetCode
- [leetcode] 50. Pow(x, n)
- Leetcode 50. Pow(x, n)
- [LeetCode]50. Pow(x, n)
- LeetCode *** 50. Pow(x, n)
- LeetCode 50. Pow(x, n)
- LeetCode 50. Pow(x, n)
- 粘性动画以及果冻效果的实现
- On the Personalities of Dead Authors
- leetcode %15 in cpp
- 计算机程序的构造和解释 练习 1.31
- 面向对象编程和函数式编程
- LeetCode 50. Pow(x, n)(求幂)
- LeetCode 51. N-Queens(N皇后)
- 登堂入室
- LeetCode 52. N-Queens II(N皇后)
- Apache Ant安装与配置
- 【华为OJ】【091-数据分类处理】
- 如何在Windows 7平台搭建Android Cocos2d-x3.2alpha0开发环境(官方)
- LeetCode 53. Maximum Subarray(最大子数组)
- 一个PsSetLoadImageNotifyRoutine回调内核注入DLL,支持xp ~ win7源码