<?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>digit Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/digit/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/digit/</link>
	<description></description>
	<lastBuildDate>Sun, 13 Aug 2023 21:41:17 +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>digit Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/digit/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2815. Max Pair Sum in an Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2815-max-pair-sum-in-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2815-max-pair-sum-in-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 13 Aug 2023 21:40:33 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[digit]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[pair]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10067</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;integer array&#160;nums. You have to find the&#160;maximum&#160;sum of a pair of numbers from&#160;nums&#160;such that the maximum&#160;digit&#160;in both numbers are equal. Return&#160;the maximum&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2815-max-pair-sum-in-an-array/">花花酱 LeetCode 2815. Max Pair Sum in an Array</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>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>. You have to find the&nbsp;<strong>maximum</strong>&nbsp;sum of a pair of numbers from&nbsp;<code>nums</code>&nbsp;such that the maximum&nbsp;<strong>digit&nbsp;</strong>in both numbers are equal.</p>



<p>Return&nbsp;<em>the maximum sum or</em>&nbsp;<code>-1</code><em>&nbsp;if no such pair exists</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [51,71,17,24,42]
<strong>Output:</strong> 88
<strong>Explanation:</strong> 
For i = 1 and j = 2, nums[i] and nums[j] have equal maximum digits with a pair sum of 71 + 17 = 88. 
For i = 3 and j = 4, nums[i] and nums[j] have equal maximum digits with a pair sum of 24 + 42 = 66.
It can be shown that there are no other pairs with equal maximum digits, so the answer is 88.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> -1
<strong>Explanation:</strong> No pair exists in nums with equal maximum digits.
</pre>



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



<ul><li><code>2 &lt;= nums.length &lt;= 100</code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li></ul>



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



<p>Enumerate all pairs of nums and check their sum and max digit.</p>



<p>Time complexity: O(n<sup>2</sup>)<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 maxSum(vector&lt;int&gt;&amp; nums) {
    auto maxDigit = [](int x) {
      int ans = 0;
      while (x) {
        ans = max(ans, x % 10);
        x /= 10;
      }
      return ans;
    };
    int ans = -1;
    const int n = nums.size();
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        if (nums[i] + nums[j] &gt; ans 
            &amp;&amp; maxDigit(nums[i]) == maxDigit(nums[j]))
          ans = nums[i] + nums[j];
    return ans;
  }
};</pre>
</div></div>

<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2815-max-pair-sum-in-an-array/">花花酱 LeetCode 2815. Max Pair Sum in an Array</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/algorithms/array/leetcode-2815-max-pair-sum-in-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 65. Valid Number</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-65-valid-number/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-65-valid-number/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 27 Nov 2021 05:13:03 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[digit]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8805</guid>

					<description><![CDATA[<p>A&#160;valid number&#160;can be split up into these components (in order): A&#160;decimal number&#160;or an&#160;integer. (Optional) An&#160;'e'&#160;or&#160;'E', followed by an&#160;integer. A&#160;decimal number&#160;can be split up into these&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-65-valid-number/">花花酱 LeetCode 65. Valid 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>A&nbsp;<strong>valid number</strong>&nbsp;can be split up into these components (in order):</p>



<ol><li>A&nbsp;<strong>decimal number</strong>&nbsp;or an&nbsp;<strong>integer</strong>.</li><li>(Optional) An&nbsp;<code>'e'</code>&nbsp;or&nbsp;<code>'E'</code>, followed by an&nbsp;<strong>integer</strong>.</li></ol>



<p>A&nbsp;<strong>decimal number</strong>&nbsp;can be split up into these components (in order):</p>



<ol><li>(Optional) A sign character (either&nbsp;<code>'+'</code>&nbsp;or&nbsp;<code>'-'</code>).</li><li>One of the following formats:<ol><li>One or more digits, followed by a dot&nbsp;<code>'.'</code>.</li><li>One or more digits, followed by a dot&nbsp;<code>'.'</code>, followed by one or more digits.</li><li>A dot&nbsp;<code>'.'</code>, followed by one or more digits.</li></ol></li></ol>



<p>An&nbsp;<strong>integer</strong>&nbsp;can be split up into these components (in order):</p>



<ol><li>(Optional) A sign character (either&nbsp;<code>'+'</code>&nbsp;or&nbsp;<code>'-'</code>).</li><li>One or more digits.</li></ol>



<p>For example, all the following are valid numbers:&nbsp;<code>["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]</code>, while the following are not valid numbers:&nbsp;<code>["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]</code>.</p>



<p>Given a string&nbsp;<code>s</code>, return&nbsp;<code>true</code><em>&nbsp;if&nbsp;</em><code>s</code><em>&nbsp;is a&nbsp;<strong>valid number</strong></em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "0"
<strong>Output:</strong> true
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "e"
<strong>Output:</strong> false
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "."
<strong>Output:</strong> false
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = ".1"
<strong>Output:</strong> true
</pre>



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



<ul><li><code>1 &lt;= s.length &lt;= 20</code></li><li><code>s</code>&nbsp;consists of only English letters (both uppercase and lowercase), digits (<code>0-9</code>), plus&nbsp;<code>'+'</code>, minus&nbsp;<code>'-'</code>, or dot&nbsp;<code>'.'</code>.</li></ul>



<h2><strong>Solution: Rule checking</strong></h2>



<p>Time complexity: O(n)<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:
  bool isNumber(string s) {
    for (int i = 0; i &lt; s.length(); ++i)
      if (s[i] != ' ') {
        s = s.substr(i);
        break;
      }
    while (!s.empty() &amp;&amp; s.back() == ' ') s.pop_back();
    
    bool dot = false;
    bool e = false;
    bool digit = false;
    for (int i = 0; i &lt; s.length(); ++i) {
      s[i] = tolower(s[i]);
      if (isdigit(s[i])) {
        digit = true;
      } else if (s[i] == '.') {
        if (e || dot) return false;
        dot = true;
      } else if (s[i] == 'e') {
        if (e || !digit) return false;
        e = true;
        digit = false;
      } else if (s[i] == '-' || s[i] == '+') {
        if (i != 0 &amp;&amp; s[i - 1] != 'e')
          return false;
      } else {
        return false;
      }
    }
    return digit;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-65-valid-number/">花花酱 LeetCode 65. Valid 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/string/leetcode-65-valid-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 728. Self Dividing Numbers</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-728-self-dividing-numbers/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-728-self-dividing-numbers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 19 Nov 2017 16:13:34 +0000</pubDate>
				<category><![CDATA[Easy]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[digit]]></category>
		<category><![CDATA[mod]]></category>
		<category><![CDATA[reminder]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=845</guid>

					<description><![CDATA[<p>Problem: A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because 128 % 1 == 0, 128&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-728-self-dividing-numbers/">花花酱 LeetCode 728. Self Dividing Numbers</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/Px8TABDC7ag?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>A <i>self-dividing number</i> is a number that is divisible by every digit it contains.</p>
<p>For example, 128 is a self-dividing number because <code>128 % 1 == 0</code>, <code>128 % 2 == 0</code>, and <code>128 % 8 == 0</code>.</p>
<p>Also, a self-dividing number is not allowed to contain the digit zero.</p>
<p>Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: 
left = 1, right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]</pre><p><b>Note:</b></p>
<ul>
<li>The boundaries of each input argument are <code>1 &lt;= left &lt;= right &lt;= 10000</code>.</li>
</ul>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<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><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<strong>Idea:</strong></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/728-ep111-1.png"><img class="alignnone size-full wp-image-851" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/728-ep111-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/728-ep111-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/728-ep111-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/728-ep111-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/728-ep111-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p>Brute Force</p>
<p>Time Complexity: O(n)</p>
<p>Space Complexity: O(1)</p>
<p>&nbsp;</p>
<p><strong>Solution:</strong></p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 3 ms
class Solution {
public:
    vector&lt;int&gt; selfDividingNumbers(int left, int right) {
        vector&lt;int&gt; ans;
        for (int n = left; n &lt;= right; ++n) {
            int t = n;
            bool valid = true;
            while (t &amp;&amp; valid) {
                const int r = t % 10;
                if (r == 0 || n % r)
                    valid = false;
                t /= 10;
            }
            if (valid) ans.push_back(n);
        }
        return ans;
    }
};</pre><p>String</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 6 ms
class Solution {
public:
    vector&lt;int&gt; selfDividingNumbers(int left, int right) {
        vector&lt;int&gt; ans;
        for (int n = left; n &lt;= right; ++n) {
            const string t = std::to_string(n);
            bool valid = true;
            for (const char c : t) {
                if (c == '0' || n % (c - '0')) {
                    valid = false;
                    break;
                }
            }
            if (valid) ans.push_back(n);
        }
        return ans;
    }
};</pre><p>&nbsp;</p>
<p><strong>Related Problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/math/leetcode-611-valid-triangle-number/">[解题报告] LeetCode 611. Valid Triangle Number</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-728-self-dividing-numbers/">花花酱 LeetCode 728. Self Dividing Numbers</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-728-self-dividing-numbers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
