<?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>high precision Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/high-precision/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/high-precision/</link>
	<description></description>
	<lastBuildDate>Sat, 14 Mar 2020 07:45:36 +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>high precision Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/high-precision/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 306. Additive Number</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-306-additive-number/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-306-additive-number/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 14 Mar 2020 07:38:00 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[high precision]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6477</guid>

					<description><![CDATA[<p>Additive number is a string whose digits can form additive sequence. A valid additive sequence should contain&#160;at least&#160;three numbers. Except for the first two numbers,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-306-additive-number/">花花酱 LeetCode 306. Additive Number</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>Additive number is a string whose digits can form additive sequence.</p>



<p>A valid additive sequence should contain&nbsp;<strong>at least</strong>&nbsp;three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.</p>



<p>Given a string containing only digits&nbsp;<code>'0'-'9'</code>, write a function to determine if it&#8217;s an additive number.</p>



<p><strong>Note:</strong>&nbsp;Numbers in the additive sequence&nbsp;<strong>cannot</strong>&nbsp;have leading zeros, so sequence&nbsp;<code>1, 2, 03</code>&nbsp;or&nbsp;<code>1, 02, 3</code>&nbsp;is invalid.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> "112358"
<strong>Output:</strong> true
<strong>Explanation:</strong> The digits can form an additive sequence: 1, 1, 2, 3, 5, 8. 
&nbsp;            1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> "199100199"
<strong>Output:</strong> true
<strong>Explanation:</strong> The additive sequence is: 1, 99, 100, 199.&nbsp;
&nbsp;            1 + 99 = 100, 99 + 100 = 199
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>num</code>&nbsp;consists only of digits&nbsp;<code>'0'-'9'</code>.</li><li><code>1 &lt;= num.length &lt;= 35</code></li></ul>



<h2><strong>Solution: DFS</strong></h2>



<p>Time complexity: O(n^2)<br>Space complexity: O(n)</p>



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

<pre class="crayon-plain-tag">using SV = std::string_view;
class Solution {
public:
  bool isAdditiveNumber(SV s) {    
    auto add = [](SV s1, SV s2) {
      const int l1 = s1.length(), l2 = s2.length();
      string s3(max(l1, l2) + 1, '0');      
      for (int i = 0; i &lt; s3.size() - 1; ++i) {
        int n1 = i &lt; l1 ? s1[l1 - i - 1] - '0' : 0;
        int n2 = i &lt; l2 ? s2[l2 - i - 1] - '0' : 0;
        s3[i] += n1 + n2;
        if (s3[i] &gt; '9') {
          s3[i] -= 10;
          ++s3[i + 1];
        }
      }
      while (s3.back() == '0' &amp;&amp; s3.length() &gt; 1) s3.pop_back();
      return string{rbegin(s3), rend(s3)};
    };
    
    auto isValid = [](SV s) { return s.length() == 1 || s[0] != '0'; };
    
    function&lt;bool(SV, SV, SV)&gt; additive = [&amp;](SV s1, SV s2, SV right) {
      if (!isValid(s1) || !isValid(s2)) return false;      
      string s3 = add(s1, s2);
      const int l3 = s3.length();
      if (right.substr(0, l3) != s3) return false;
      if (right.length() == l3) return true;
      return additive(s2, s3, right.substr(l3));
    };
    
    for (int i = 1; i &lt;= s.size(); ++i)
      for (int j = 1; i + j &lt;= s.size(); ++j)
        if (additive(s.substr(0, i), s.substr(i, j), s.substr(i + j))) 
          return true;
    return false;
  }
};</pre>
</div></div>



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

<pre class="crayon-plain-tag">class Solution:
  def isAdditiveNumber(self, num: str) -&gt; bool:
    n = len(num)
    
    def valid(s): return len(s) == 1 or s[0] != '0'
    
    def additive(s1, s2, right):
      if not valid(s1) or not valid(s2): return False
      s3 = str(int(s1) + int(s2))
      if right.startswith(s3):
        if right == s3: return True
        return additive(s2, s3, right[len(s3):])
      return False
      
    for l1 in range(1, n // 2 + 1):
      for l2 in range(1, n + 1 - l1):
        if additive(num[:l1], num[l1:l1+l2], num[l1+l2:]):
          return True
    return False</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-306-additive-number/">花花酱 LeetCode 306. Additive Number</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/searching/leetcode-306-additive-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 989. Add to Array-Form of Integer</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-989-add-to-array-form-of-integer/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-989-add-to-array-form-of-integer/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Feb 2019 09:12:20 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[high precision]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4817</guid>

					<description><![CDATA[<p>For a non-negative integer&#160;X, the&#160;array-form of&#160;X&#160;is an array of its digits in left to right order.&#160; For example, if&#160;X = 1231, then the array form&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-989-add-to-array-form-of-integer/">花花酱 LeetCode 989. Add to Array-Form of Integer</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>For a non-negative integer&nbsp;<code>X</code>, the&nbsp;<em>array-form of&nbsp;<code>X</code></em>&nbsp;is an array of its digits in left to right order.&nbsp; For example, if&nbsp;<code>X = 1231</code>, then the array form is&nbsp;<code>[1,2,3,1]</code>.</p>



<p>Given the array-form&nbsp;<code>A</code>&nbsp;of a non-negative&nbsp;integer&nbsp;<code>X</code>, return the array-form of the integer&nbsp;<code>X+K</code>.</p>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>A = [1,2,0,0], K = 34
<strong>Output: </strong>[1,2,3,4]
<strong>Explanation: </strong>1200 + 34 = 1234
</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>A = [2,7,4], K = 181
<strong>Output: </strong>[4,5,5]
<strong>Explanation: </strong>274 + 181 = 455
</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>A = [2,1,5], K = 806
<strong>Output: </strong>[1,0,2,1]
<strong>Explanation: </strong>215 + 806 = 1021
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>A = [9,9,9,9,9,9,9,9,9,9], K = 1
<strong>Output: </strong>[1,0,0,0,0,0,0,0,0,0,0]
<strong>Explanation: </strong>9999999999 + 1 = 10000000000
</pre>



<p><strong>Note：</strong></p>



<ol><li><code>1 &lt;= A.length &lt;= 10000</code></li><li><code>0 &lt;= A[i] &lt;= 9</code></li><li><code>0 &lt;= K &lt;= 10000</code></li><li>If&nbsp;<code>A.length &gt; 1</code>, then&nbsp;<code>A[0] != 0</code></li></ol>



<h2>Solution: Simulation</h2>
Time complexity: O(n)
Space complexity: O(n)



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

<pre class="crayon-plain-tag">// Author: Huahua, running time: 136 ms, 10.1 MB
class Solution {
public:
  vector&lt;int&gt; addToArrayForm(vector&lt;int&gt;&amp; A, int K) {
    vector&lt;int&gt; ans;
    ans.reserve(A.size() + 1);    
    for (int i = A.size() - 1; i &gt;= 0 || K &gt; 0; --i) {
      K += (i &gt;= 0 ? A[i] : 0);
      ans.push_back(K % 10);
      K /= 10;
    }    
    reverse(begin(ans), end(ans));
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-989-add-to-array-form-of-integer/">花花酱 LeetCode 989. Add to Array-Form of Integer</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/simulation/leetcode-989-add-to-array-form-of-integer/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
