前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++版 - 剑指Offer 面试题11:数的整数次方(Leetcode50. Pow(x, n))【C库函数pow模拟】题解

C++版 - 剑指Offer 面试题11:数的整数次方(Leetcode50. Pow(x, n))【C库函数pow模拟】题解

作者头像
Enjoy233
发布2019-03-05 14:22:03
1K0
发布2019-03-05 14:22:03
举报

面试题:数的整数次方

温馨提示:本技术博客的相关代码将会在github(https://github.com/yanglr)中同步更新,敬请star和fork...

题目:实现函数double Power(double base, int exponent), 求base的exponent次方。不得使用库函数,同时不需要考虑大数问题

其中base为浮点数,而exponent为整数(可正可负,可为0).

提交网址: http://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00?tpId=13&tqId=11165

分析:

二分求幂(时间复杂度为log n),使用二分法则问题可转化为:

此公式并不能很好的反映情况,且用n=2k+1表示奇数时,

故以上公式可改进成如下公式:

其中

不论n是奇是偶,对其折半后下取整即可得到k,而在编程语言中如果变量类型为整型,k=n/2,如果用弱数据类型的语言(比如:PHP、Python、JavaScript等)实现此方法,需自行ceiling或ceil进行下取整!

当选取某种强数据类型的编程语言,且上述公式中的变量n类型为整型时,可继续简化:

这样,比较容易理解,也有利于写出清晰结构的代码...

非递归实现 AC代码:

代码语言:javascript
复制
#include<cstdio>
using namespace std;
class Solution {
public:
    double Power(double base, int exponent) {
        if(exponent < 0)  // 将指数exponent<0的情形规约为exponent>0的情形
        {
            exponent = -exponent;
            base = 1.0/base;
        }
        double res = 1.0;
        for(; exponent > 0; exponent >>= 1)  // 指数每次折半,进行迭代
        {
            if((exponent & 0x1) == 1)  res *= base;  // a & 0x1 等价于 a%2,用来判断a的奇偶性, 位运算优先级低于关系运算符, 记得加括号!
            base *= base;  // 底数base替换为base^2
        }
        return res;
    }
};

// 以下为测试
int main()
{
	Solution sol;
	double num1=sol.Power(20, 2);
	double num2=sol.Power(13, -2);
	double num3=sol.Power(-10, 3);
	double num4=sol.Power(0, 0);	
	printf("%f\n",num1); 
	printf("%f\n",num2);
	printf("%f\n",num3);		
	printf("%d\n",num4);		
	return 0;
}

剑指offer上不需要考虑大数问题,可是Leetcode上需要考虑大数问题、边界值问题...

50. Pow(x, n)

Total Accepted: 90298 Total Submissions: 323977 Difficulty: Medium    ACrate: 27.9%

Implement pow(x, n).

提交网址: https://leetcode.com/problems/powx-n/

刚开始Leetcode上越界条件怎么都过不去,每次都是 299 / 300 test cases passed....

输入输出样例:

Input: 2.00000 -2147483648 Output: 0.00000 ---------------- Input: 1.00000 -2147483648 Output: 1.00000 ---------------- Input: -1.00000 2147483647 Output: -1.00000 ---------------- Input: -1.00000 -2147483648 Output: 1.00000 ---------------- Input: 2.00000 -2147483648 Output: 0.00000 LeetCode 已AC代码:

代码语言:javascript
复制
#include<cstdio>
#include<climits>     // INT_MAX和INT_MAX在C语言limits.h中定义,而float.h 定义了double、float类型数的最大最小值 
using namespace std;
class Solution {
public:
    double myPow(double x, int n)
    {
        if(x==0 && n==0) return 1;    
        if((n<=INT_MIN || n>=INT_MAX) && (x>1 || x<-1)) return 0;
        if(x==1 && n==INT_MIN) return 1;  // 加的超强补丁   
        if(n>=INT_MAX && (x==1 || x==-1)) return x;
        if(n<=INT_MIN && (x==1 || x==-1)) return -x;

        if(n < 0)
        {
            n = -n;
            x = 1.0/x;
        }
        double res = 1.0;
        for(; n > 0; n >>= 1)
        {
            if((n & 0x1) == 1)  res *= x;
            x *= x;  // 底数x替换为x^2
        }
        return res;
    }
};

// 以下为测试
int main()
{
	Solution sol;
	double res1=sol.myPow(0, 0);	
	double res2=sol.myPow(13, -2);
	double res3=sol.myPow(-10, 0);
	double res4=sol.myPow(20, 2);	
	double res5=sol.myPow(2.00000,-2147483648);
	double res6=sol.myPow(1.00000,-2147483648);
	double res7=sol.myPow(-1.00000,2147483647);
	double res8=sol.myPow(-1.00000,-2147483648);
	double res9=sol.myPow(2.00000,-2147483648);
		
	printf("%f\n", res1); 
	printf("%f\n", res2);
	printf("%f\n", res3);		
	printf("%f\n", res4);
	printf("%f\n", res5);
	printf("%f\n", res6);		
	printf("%f\n", res7);
	printf("%f\n", res8);
	printf("%f\n", res9);				
	return 0;
}

相关链接:

剑指offer-面试题11:数值的整数次方 https://www.douban.com/note/355223813/

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年05月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档