【LeetCode】50. Pow(x, n)
来源:互联网 发布:mac怎么登陆千牛 编辑:程序博客网 时间:2024/06/06 02:45
题目:
50. Pow(x, n)
DescriptionHintsSubmissionsSolutions
- Total Accepted: 143792
- Total Submissions: 539052
- Difficulty: Medium
- Contributor: LeetCode
Implement pow(x, n).
Subscribe to see which companies asked this question.
解题思路:pow(x,n)即n个x相乘,用二分的思想,当n为偶数时,可以表示为pow(x,n/2)*pow(x,n/2),当n为基数时,可以表示为pow(x,n/2)*pow(x,n/2)*x.
解答:
class Solution {
public:
double myPow(double x, int n) {
if(n < 0){
return 1.0/myPow1(x,-n);
}
else{
return myPow1(x,n);
}
}
double myPow1(double x,int n){
if(n == 0){
return 1.0;
}
double y = myPow1(x,n/2);
if(n & 1){
return y*y*x;
}
else{
return y*y;
}
}
};
1 0
- [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)
- 【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)
- Android ToolBar 完全解析
- 进程peb结构、获得peb的方法
- Restful后台系统搭建(一)
- js为操作radio
- socket编程入门(一)
- 【LeetCode】50. Pow(x, n)
- swf逆向调试
- 知识点2:阐述面向对象的三大特征
- .class 概述 jvm
- spring源码附录(5)解析子元素constructor-arg
- 手推adaBoost
- Linux指令
- 湖北民族学院oj 1775(贪心)(杭电1009) 之 FatMouse's Trade
- 关于Linux下源码安装