<?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>newton Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/newton/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/newton/</link>
	<description></description>
	<lastBuildDate>Mon, 03 Sep 2018 17:17:59 +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>newton Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/newton/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode. 69 Sqrt(x)</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-69-sqrtx/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-69-sqrtx/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 17 Jan 2018 02:41:12 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[Easy]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[newton]]></category>
		<category><![CDATA[sqrt]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1613</guid>

					<description><![CDATA[<p>题目大意：让你实现开根号函数，只需要返回整数部分。 Problem: Implement int sqrt(int x). Compute and return the square root of x. x is guaranteed to be a non-negative integer. Example 1: [crayon-663c8b3e47b2d805783337/] Example 2: [crayon-663c8b3e47b32530907682/]&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-69-sqrtx/">花花酱 LeetCode. 69 Sqrt(x)</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><iframe width="500" height="375" src="https://www.youtube.com/embed/_K4f9I11hYI?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>题目大意：让你实现开根号函数，只需要返回整数部分。</p>
<p><strong>Problem:</strong></p>
<p>Implement <code>int sqrt(int x)</code>.</p>
<p>Compute and return the square root of <i>x</i>.</p>
<p><b>x</b> is guaranteed to be a non-negative integer.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: 4
Output: 2</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842...</pre><p>&nbsp;</p>
<p><img class="alignnone size-full wp-image-1624" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/69-ep158.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/69-ep158.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/69-ep158-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/69-ep158-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><img class="alignnone size-full wp-image-1628" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/69-ep158-2-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/69-ep158-2-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/69-ep158-2-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/69-ep158-2-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"> </ins></p>
<h1><strong>Solution 1: Brute force</strong></h1>
<p>Time complexity: sqrt(x)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 40 ms
class Solution {
public:
  int mySqrt(int x) {
    if (x &lt;= 1) return x;
    for (long long s = 1; s &lt;= x; ++s)
      if (s * s &gt; x) return s - 1;
    return -1;
  }
};</pre><p></div><h2 class="tabtitle">C++ div</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 162 ms
class Solution {
public:
  int mySqrt(int x) {
    if (x &lt;= 1) return x;
    for (int s = 1; s &lt;= x; ++s)
      if (s &gt; x / s) return s - 1;
    return -1;
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 71 ms
class Solution {
  public int mySqrt(int x) {
    if (x &lt;= 1) return x;
    for (long s = 1; s &lt;= x; ++s)
      if (s * s &gt; x) return (int)s - 1;
    return -1;
  }
}</pre><p></div><h2 class="tabtitle">Python3 TLE</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
TLE: 602 / 1017 test cases passed.
"""
class Solution:
  def mySqrt(self, x):
    if x &lt;= 1: return x
    s = 1
    while True:
      if s*s &gt; x: return s - 1
      s += 1
    return -1</pre><p></div></div></p>
<h1><strong>Solution 2: Binary search</strong></h1>
<p>Time complexity: O(logn)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 12 ms
class Solution {
public:
  int mySqrt(int x) {      
    long l = 1;
    long r = static_cast&lt;long&gt;(x) + 1;
    while (l &lt; r) {
      long m = l + (r - l) / 2;
      if (m * m &gt; x) { 
        r = m;
      } else {
        l = m + 1;
      }
    }
    // l: smallest number such that l * l &gt; x
    return l - 1;
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 47 ms
class Solution {
  public int mySqrt(int x) {      
    int l = 1;
    int r = x;
    while (l &lt;= r) {
      int m = l + (r - l) / 2;
      if (m &gt; x / m) {
        r = m - 1;
      } else {
        l = m + 1;
      }
    }
    return r;
  }
}</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 119 ms
"""
class Solution:
  def mySqrt(self, a):
    l = 1
    r = a
    while l &lt;= r:
      m = l + (r - l) // 2
      if m * m &gt; a:
        r = m - 1
      else:
        l = m + 1
    return r;</pre><p></div></div></p>
<h1><strong>Solution 3: Newton&#8217;s method</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++ / float</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 28 ms
class Solution {
public:
    int mySqrt(int a) {
      constexpr double epsilon = 1e-2;
      double x = a;
      while (x * x - a &gt; epsilon) {
        x = (x + a / x) / 2.0;
      }
      return x;
    }
};</pre><p></div><h2 class="tabtitle">C++ / int</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 27 ms
class Solution {
public:
  int mySqrt(int a) {
    // f(x) = x^2 - a, find root of f(x)
    // Newton's method
    // f'(x) = 2x
    // x' = x - f(x) / f'(x) = x - (1/2*x - a/(2*x))
    //    = (x + a / x) / 2
    int x = a;
    while (x &gt; a / x)
      x = (x + a / x) / 2;
    return x;
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 44 ms
class Solution {
  public int mySqrt(int a) {    
    long x = a;
    while (x * x &gt; a)
      x = (x + a / x) / 2;    
    return (int)x;
  }
}</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 100 ms
"""
class Solution:
  def mySqrt(self, a):
    x = a
    while x * x &gt; a:
      x = (x + a // x) // 2
    return x</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-69-sqrtx/">花花酱 LeetCode. 69 Sqrt(x)</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-69-sqrtx/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
