{"id":5104,"date":"2019-04-24T07:12:51","date_gmt":"2019-04-24T14:12:51","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5104"},"modified":"2019-04-24T07:14:40","modified_gmt":"2019-04-24T14:14:40","slug":"leetcode-50-powx-n","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-50-powx-n\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 50. Pow(x, n)"},"content":{"rendered":"\n<p>Implement&nbsp;<a href=\"http:\/\/www.cplusplus.com\/reference\/valarray\/pow\/\" target=\"_blank\" rel=\"noreferrer noopener\">pow(<em>x<\/em>,&nbsp;<em>n<\/em>)<\/a>, which calculates&nbsp;<em>x<\/em>&nbsp;raised to the power&nbsp;<em>n<\/em>&nbsp;(x<sup>n<\/sup>).<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 2.00000, 10\n<strong>Output:<\/strong> 1024.00000\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 2.10000, 3\n<strong>Output:<\/strong> 9.26100\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 2.00000, -2\n<strong>Output:<\/strong> 0.25000\n<strong>Explanation:<\/strong> 2<sup>-2<\/sup> = 1\/2<sup>2<\/sup> = 1\/4 = 0.25\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>-100.0 &lt;&nbsp;<em>x<\/em>&nbsp;&lt; 100.0<\/li><li><em>n<\/em>&nbsp;is a 32-bit signed integer, within the range&nbsp;[\u22122<sup>31<\/sup>,&nbsp;2<sup>31&nbsp;<\/sup>\u2212 1]<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>square x and cut n in half. <br>if n is negative, compute 1.0 \/ pow(x, |n|)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">pow(x, n) := pow(x * x, n \/ 2) * (x if n % 2 else 1)<br>pow(x, 0) := 1 <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">Example:<br>pow(x, 5) = pow(x^2, 2) * x <br>          = pow(x^4, 1) * x <br>          = pow(x^8, 0) * x^4 * x<br>          = 1 * x^4 * x = x^5<br><\/pre>\n\n\n\n<p>Time complexity: O(logn)<br>Space complexity: O(logn)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  double myPow(double x, int n) {\n    return n >= 0 ? myPowImp(x, n) : 1.0 \/ myPowImp(x, n);\n  }\nprivate:\n  double myPowImp(double x, int n) {\n    if (n == 0) return 1;\n    return myPowImp(x * x, n \/ 2) * (n % 2 ? x : 1);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Implement&nbsp;pow(x,&nbsp;n), which calculates&nbsp;x&nbsp;raised to the power&nbsp;n&nbsp;(xn). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 Output: 9.26100 Example 3: Input: 2.00000, -2&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,471,17],"class_list":["post-5104","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-pow","tag-recursion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5104"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5104\/revisions"}],"predecessor-version":[{"id":5108,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5104\/revisions\/5108"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}