牛客网习题剑指offer之数值的整数次方

分析:

要考虑到exponent为0和负数的情况。

如果base是0并且exponent是负数的时候呢?那就发生除0的情况了。

AC代码:

public class Solution {

    public double Power(double base, int exponent) {

        if(exponent == 0) return 1;
        if(Math.abs(base) <= 0.000000000000001) return 0;

        boolean reverseFlag = exponent < 0;
        exponent = Math.abs(exponent);
        double result = 1;

        while(exponent-- >0){
            result *= base;
        }
        return reverseFlag ? 1 / result : result;
    }

}

参考资料:

1. https://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00?tpId=13&tqId=11165&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

时间: 2024-08-29 03:04:22

牛客网习题剑指offer之数值的整数次方的相关文章

【剑指offer】数值的整数次方

转载请注明出处:http://blog.csdn.net/ns_code/article/details/25506085 剑指offer上的第十一题,九度OJ上测试通过. 题目描述: 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 输入: 输入可能包含多个测试样例.对于每个输入文件,第一行输入一个整数T,表示测试案例的数目,接下来的T行每行输入一个浮点数base和一个整数exponent,两个数中间用一个空格隔开. 输出: 对应每

牛客网-《剑指offer》-替换空格

C++ 1 class Solution { 2 public: 3 void replaceSpace(char *str,int len) { 4 int cnt = 0; 5 for (int i = 0; i < len; i++) { 6 if (str[i] == ' ') cnt++; 7 } 8 int idx = len + cnt * 2 - 1; 9 for (int i = len - 1; i >= 0; i--) { 10 if (str[i] == ' ') {

牛客网-《剑指offer》-二维数组中的查找

C++ 1 class Solution { 2 public: 3 bool Find(vector<vector<int> > array,int target) { 4 int rows = array.size(); 5 int cols = array[0].size(); 6 int x = cols - 1; 7 int y = 0; 8 while ( x >= 0 && y < rows ) { 9 if (array[x][y] ==

牛客网-《剑指offer》-从尾到头打印链表

C++ 1 /** 2 * struct ListNode { 3 * int val; 4 * struct ListNode *next; 5 * ListNode(int x) : 6 * val(x), next(NULL) { 7 * } 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> printListFromTailToHead(struct ListNode* head) { 13 vector<int&

牛客网-《剑指offer》-调整数组顺序使奇数位于偶数前面

题目:http://www.nowcoder.com/practice/beb5aa231adc45b2a5dcc5b62c93f593 C++ 1 class Solution { 2 public: 3 void reOrderArray(vector<int> &arr) { 4 vector<int> odd; 5 vector<int> even; 6 for (auto &i: arr) { 7 if (i & 1) { 8 odd.

和为S的连续正数序列——牛客网(剑指offer)

题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22.现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck! 输出描述: 输出所有和为S的连续正数序列.序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序 题目链接 https://www.nowc

剑指offer (11) 数值的整数次方

利用create groups for any added folders 这样的方式表示的是将所有的资源都放在资源包得路径下,没有层次的概念利用create folder references for any added folders这样的表示方式是在按照原来文件夹的方式将他们放入到安装包中的.在安装包中有几个这样的文件夹:(这些文件都是位于家路径下的)(1)Documents: 该文件夹用于程序数据文件写入到该目录下,用于存储用户数据以及需要备份的数据.(2)Library:include

剑指Offer 12. 数值的整数次方 (其他)

题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 题目地址 https://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00?tpId=13&tqId=11165&tPage=1&rp=3&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-

剑指offer:数值的整数次方

题目描述给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. class Solution: def Power(self, base, exponent): # 任何数的0次方都是1 if exponent == 0: return 1 # 0的任何次方(除了0的0次方)都为0 if base == 0: return 0 # 先记录指数符号 sign = exponent > 0 exponent = abs(exponent) re