<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pow Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/pow/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/pow/</link>
	<description></description>
	<lastBuildDate>Wed, 24 Apr 2019 14:14:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>pow Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/pow/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 50. Pow(x, n)</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-50-powx-n/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-50-powx-n/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 24 Apr 2019 14:12:51 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[pow]]></category>
		<category><![CDATA[recursion]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5104</guid>

					<description><![CDATA[<p>Implement&#160;pow(x,&#160;n), which calculates&#160;x&#160;raised to the power&#160;n&#160;(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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-50-powx-n/">花花酱 LeetCode 50. Pow(x, n)</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> 2.00000, 10
<strong>Output:</strong> 1024.00000
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> 2.10000, 3
<strong>Output:</strong> 9.26100
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> 2.00000, -2
<strong>Output:</strong> 0.25000
<strong>Explanation:</strong> 2<sup>-2</sup> = 1/2<sup>2</sup> = 1/4 = 0.25
</pre>



<p><strong>Note:</strong></p>



<ul><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;[−2<sup>31</sup>,&nbsp;2<sup>31&nbsp;</sup>− 1]</li></ul>



<h2><strong>Solution: Recursion</strong></h2>



<p>square x and cut n in half. <br>if n is negative, compute 1.0 / pow(x, |n|)</p>



<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>



<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>



<p>Time complexity: O(logn)<br>Space complexity: O(logn)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  double myPow(double x, int n) {
    return n &gt;= 0 ? myPowImp(x, n) : 1.0 / myPowImp(x, n);
  }
private:
  double myPowImp(double x, int n) {
    if (n == 0) return 1;
    return myPowImp(x * x, n / 2) * (n % 2 ? x : 1);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-50-powx-n/">花花酱 LeetCode 50. Pow(x, n)</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-50-powx-n/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
