【LeetCode-面试算法经典-Java实现】【050-Implement pow(x, n)(求x的n次方)】

【050-Implement pow(x, n)(求x的n次方)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  Implement pow(x, n).

题目大意

 求x的n次方。 

解题思路

  递归求解。

代码实现

算法实现类

public class Solution {

    public double myPow(double x, int n) {
        if (x == 0 && n == 0) {
            throw new IllegalArgumentException();
        }

        // 指数正负标记
        boolean isNegative = false;

        // 求n的绝对值
        if (n < 0) {
            n = -n;
            isNegative = true;
        }

        double result = pow(x, n);

        if (isNegative) {
            return 1.0 / result;
        } else {
            return result;
        }
    }

    public double pow(double x, int n) {
        if (n == 0) {
            return 1;
        } else {
            double result = pow(x, n / 2);
            // n是奇数
            if (n % 2 != 0) {
                return x * result * result;
            } else {
                return result * result;
            }
        }
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47098373

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-21 04:48:48

【LeetCode-面试算法经典-Java实现】【050-Implement pow(x, n)(求x的n次方)】的相关文章

【LeetCode-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】

[225-Implement Stack using Queues(用队列实现栈操作)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Implement the following operations of a stack using queues. push(x) – Push element x onto stack. pop() – Removes the element on

【LeetCode-面试算法经典-Java实现】【010-Regular Expresssion Matching(正则表达式匹配)】

[010-Regular Expresssion Matching(正则表达式匹配)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element.The matching sho

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

【LeetCode-面试算法经典-Java实现】【136-Single Number(只出现一次的数字)】

[136-Single Number(只出现一次的数字)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it wit

【LeetCode-面试算法经典-Java实现】【137-Single Number II(只字出一次的数字II)】

[137-Single Number II(只出现一次的数字II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you imple

【LeetCode-面试算法经典-Java实现】【028-Implement strStr() (实现strStr()函数)】

[028-Implement strStr() (实现strStr()函数)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 题目大意 实现实现strStr()函数,判断一个字符串在另一个字符串中出现的位置.如果不匹配

【LeetCode-面试算法经典-Java实现】【008-String to Integer (atoi) (字符串转成整数)】

[008-String to Integer (atoi) (字符串转成整数)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example

【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】

[064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either