<?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>digits Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/digits/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/digits/</link>
	<description></description>
	<lastBuildDate>Mon, 09 Mar 2020 07:41:46 +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>digits Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/digits/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 258. Add Digits</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-258-add-digits/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-258-add-digits/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 09 Mar 2020 07:34:16 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[digits]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[O(1)]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6426</guid>

					<description><![CDATA[<p>Given a non-negative integer&#160;num, repeatedly add all its digits until the result has only one digit. Example: Input: 38 Output: 2 Explanation: The process is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-258-add-digits/">花花酱 LeetCode 258. Add Digits</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>Given a non-negative integer&nbsp;<code>num</code>, repeatedly add all its digits until the result has only one digit.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> <code>38</code>
<strong>Output:</strong> 2 
<strong>Explanation: </strong>The process is like: <code>3 + 8 = 11</code>, <code>1 + 1 = 2</code>. 
&nbsp;            Since <code>2</code> has only one digit, return it.
</pre>



<p><strong>Follow up:</strong><br>Could you do it without any loop/recursion in O(1) runtime?</p>



<h2><strong>Solution 1: Simulation</strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int addDigits(int num) {
    int n = num;
    while (n &gt;= 10) {      
      int t = n;
      n = 0;
      while (t) {
        n += t % 10;
        t /= 10;
      }
    }
    return n;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Math</strong></h2>



<p><a href="https://en.wikipedia.org/wiki/Digital_root#Congruence_formula">https://en.wikipedia.org/wiki/Digital_root#Congruence_formula</a></p>



<p>Digit root = num % 9 if num % 9 != 0 else min(num, 9) e.g. 0 or 9</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int addDigits(int num) {    
    return num % 9 ? num % 9 : min(num, 9);
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-258-add-digits/">花花酱 LeetCode 258. Add Digits</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-258-add-digits/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1317. Convert Integer to the Sum of Two No-Zero Integers</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1317-convert-integer-to-the-sum-of-two-no-zero-integers/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1317-convert-integer-to-the-sum-of-two-no-zero-integers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 14 Jan 2020 05:29:34 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[digits]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[O(nlogn)]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6090</guid>

					<description><![CDATA[<p>Given an integer&#160;n. No-Zero integer is a positive integer which&#160;doesn&#8217;t contain any 0&#160;in its decimal representation. Return&#160;a list of two integers&#160;[A, B]&#160;where: A&#160;and&#160;B&#160;are No-Zero integers.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1317-convert-integer-to-the-sum-of-two-no-zero-integers/">花花酱 LeetCode 1317. Convert Integer to the Sum of Two No-Zero Integers</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>Given an integer&nbsp;<code>n</code>. No-Zero integer is a positive integer which&nbsp;<strong>doesn&#8217;t contain any 0</strong>&nbsp;in its decimal representation.</p>



<p>Return&nbsp;<em>a list of two integers</em>&nbsp;<code>[A, B]</code>&nbsp;where:</p>



<ul><li><code>A</code>&nbsp;and&nbsp;<code>B</code>&nbsp;are No-Zero integers.</li><li><code>A + B = n</code></li></ul>



<p>It&#8217;s guarateed that there is at least one valid solution. If there are many valid solutions you can return any of them.</p>



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



<pre class="crayon-plain-tag">&lt;strong&gt;Input:&lt;/strong&gt; n = 2
&lt;strong&gt;Output:&lt;/strong&gt; [1,1]
&lt;strong&gt;Explanation:&lt;/strong&gt; A = 1, B = 1. A + B = n and both A and B don't contain any 0 in their decimal representation.</pre>



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



<pre class="crayon-plain-tag">&lt;strong&gt;Input:&lt;/strong&gt; n = 11
&lt;strong&gt;Output:&lt;/strong&gt; [2,9]</pre>



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



<pre class="crayon-plain-tag">&lt;strong&gt;Input:&lt;/strong&gt; n = 10000
&lt;strong&gt;Output:&lt;/strong&gt; [1,9999]</pre>



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



<pre class="crayon-plain-tag">&lt;strong&gt;Input:&lt;/strong&gt; n = 69
&lt;strong&gt;Output:&lt;/strong&gt; [1,68]</pre>



<p><strong>Example 5:</strong></p>



<pre class="crayon-plain-tag">&lt;strong&gt;Input:&lt;/strong&gt; n = 1010
&lt;strong&gt;Output:&lt;/strong&gt; [11,999]</pre>



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



<ul><li><code>2 &lt;= n &lt;= 10^4</code></li></ul>



<h2><strong>Solution: Brute Force</strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; getNoZeroIntegers(int n) {
    auto valid = [](int x) {
      if (!x) return false;
      while (x) {
        if (x % 10 == 0) return false;
        x /= 10;
      }
      return true;
    };
    
    for (int i = 1; i &lt;= n / 2; ++i)
      if (valid(i) &amp;&amp; valid(n - i)) 
        return {i, n - i};
    return {};
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1317-convert-integer-to-the-sum-of-two-no-zero-integers/">花花酱 LeetCode 1317. Convert Integer to the Sum of Two No-Zero Integers</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-1317-convert-integer-to-the-sum-of-two-no-zero-integers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1295. Find Numbers with Even Number of Digits</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1295-find-numbers-with-even-number-of-digits/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1295-find-numbers-with-even-number-of-digits/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 27 Dec 2019 06:58:37 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[digits]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5975</guid>

					<description><![CDATA[<p>Given an array&#160;nums&#160;of integers, return how many of them contain an&#160;even number&#160;of digits. Example 1: Input: nums = [12,345,2,6,7896] Output: 2 Explanation: 12 contains 2&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1295-find-numbers-with-even-number-of-digits/">花花酱 LeetCode 1295. Find Numbers with Even Number of Digits</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>Given an array&nbsp;<code>nums</code>&nbsp;of integers, return how many of them contain an&nbsp;<strong>even number</strong>&nbsp;of digits.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [12,345,2,6,7896]
<strong>Output:</strong> 2
<strong>Explanation: 
</strong>12 contains 2 digits (even number of digits).&nbsp;
345 contains 3 digits (odd number of digits).&nbsp;
2 contains 1 digit (odd number of digits).&nbsp;
6 contains 1 digit (odd number of digits).&nbsp;
7896 contains 4 digits (even number of digits).&nbsp;
Therefore only 12 and 7896 contain an even number of digits.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [555,901,482,1771]
<strong>Output:</strong> 1 
<strong>Explanation: </strong>
Only 1771 contains an even number of digits.
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 500</code></li><li><code>1 &lt;= nums[i] &lt;= 10^5</code></li></ul>



<h2><strong>Solution: Math</strong></h2>



<p>Time complexity: O(n * log(max(num)))<br>Space complexity: O(1)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int findNumbers(vector&lt;int&gt;&amp; nums) {
    return count_if(begin(nums), end(nums), [](int num) {
      int d = 0;
      do { ++d; } while (num /= 10);
      return d % 2 == 0;
    });
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def findNumbers(self, nums: List[int]) -&gt; int:
    return sum([len(str(x)) % 2 == 0 for x in nums])</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1295-find-numbers-with-even-number-of-digits/">花花酱 LeetCode 1295. Find Numbers with Even Number of Digits</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-1295-find-numbers-with-even-number-of-digits/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
