Leetcode
  • Leetcode Questions
  • Runtime Screenshots
  • DataStructure
    • Leetcode 394. Decode String
    • Leetcode 225. Implement Stack Using Queues
    • Leetcode 336. Palindrome Pairs
    • Leetcode 316. Remove Duplicate Letters
    • Leetcode 206. Reverse Linked List
    • Leetcode 347. Top K Frequent Elements
    • Leetcode 227. Basic Calculator II
    • Leetcode 224. Basic Calculator
  • Linear
    • Leetcode 23. Merge k Sorted Lists
    • Leetcode 48. Rotate Image
    • Leetcode 6. ZigZag Conversion
    • Leetcode 438. Find All Anagrams in a String
    • Leetcode 189. Rotate Array
    • LeetCode 56. Merge Intervals
    • Leetcode 4. Median of Two Sorted Array
    • Leetcode 3. Longest Substring Without Repeating Characters
    • Leetcode 8. String to Integer (atoi)
    • Leetcode 5. Longest Palindromic Substring
    • Leetcode 11. Container With Most Water
  • Tree
    • Leetcode 103. Binary Tree Zigzag Level Order Traversal
    • Leetcode 508. Most Frequent Subtree Sum
    • Leetcode 226. Invert Binary Tree
    • Leetcode 222. Count Complete Tree Nodes
    • Leetcode 250. Count Univalue Subtrees
    • Leetcode 285. Inorder Successor in BST
    • Leetcode 230. Kth Smallest Element in a BST
    • Leetcode 543. Diameter of Binary Tree
    • Leetcode 199. Binary Tree Right Side View
  • Math
    • Leetcode 50. Power(x, n)
    • Leetcode 166. Fraction to Recurring Decimal
    • Leetcode 7. Reverse Integer
    • Leetcode 360. Sort Transformed Array
    • Leetcode 367. Valid Perfect Square
    • Leetcode 12. Integer to Roman
  • DynamicProgramming
    • Leetcode 10. Regular Expression Matching
    • Leetcode 253. Meeting Rooms II
    • Leetcode 303. Range Sum Query
    • Leetcode 22. Generate Parentheses
  • Graph
    • Leetcode 142. Linked List Cycle II
    • Leetcode 261. Graph Valid Tree
    • Leetcode 339. Nested List Weight Sum
    • Leetcode 207. Course Schedule
  • OODesign
    • Leetcode 295. Find Media From Data Stream
Powered by GitBook
On this page
  • 题目
  • 思路
  • 解答
  • Complexity Analysis
  • 拓展
  • 总结
  • Reference

Was this helpful?

  1. Math

Leetcode 50. Power(x, n)

题目

Calculate pow(x, n) which is x raised to the power of n.

Example 1:

Input: 2.00000, 10
Output: 1024.00000

Example 2:

Input: 2.10000, 3
Output: 9.26100

Example 3:

Input: 2.00000, -2
Output: 0.25000
Explanation: 2^(-2) = (1/2)^2 = 1/4 = 0.25

Note:

  • -100.0 < x < 100.0

  • n is a 32-bit signed integer, within the range [(-2)^31, 2^31 - 1]

思路

这道题最直接的思路为用循环来乘n次x来得到结果。即使在n为负的情况下,我们仅需将n替换为-n,将x替换为1/x. 这种方法时间复杂度为O(n),显然在题目给定的range上,这个方法太慢了。

我们可以结合简单的数学来对以上算法进行优化。我们注意到:

if n is even: 
    x^n = x^(n / 2) * x^(n / 2)
if n is odd:
    x^n = x^( (n-1) / 2) * x^( (n-1) / 2)  * x

这提示我们可以将pow(x, n)化分为性质相同、大小减半的子问题来解决,即Divide and Conquer(分治)的思想。 我们可以用递归来实现这个算法,如下

解答

class Solution {
    public double myPow(double x, int n) { 
        long N = n;     // prevent integer overflow
        if (N < 0) {
            N = -N;
            x = 1.0 / x;
        }
        return fastPow(x, N);
    }
    private double fastPow(double x, long n) {
        if (n == 0) return 1;
        double half = fastPow(x, n / 2);
        if (n % 2 == 0) 
            return half * half;
        else
            return half * half * x;
    }
}

Complexity Analysis

  • Time Complexity: O(log(n)). 我们每次递归都将问题大小缩小为了原来的一半,因此总共需要O(log(n))时间。

  • Space Complexity: O(log(n)). 我们需要O(log(n))的stack space因为做了这么多次的递归操作。

拓展

我们主要到递归占用了非常多的stack space。为了节省空间,我们可以用循环来实现这个算法吗?

总结

这种看似简单的题目其实深坑非常多。首先,我们需要考虑我们解法的性能,比如时间空间复杂度。其次,我们还需要考虑 各种极端的边界情况,比如Integer overflow等等问题。这道题目中我们就使用了long来避免这个错误。 这需要大量的积累与总结。

Reference

PreviousMathNextLeetcode 166. Fraction to Recurring Decimal

Last updated 5 years ago

Was this helpful?

Leetcode Official Solution