mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
34 lines
1.4 KiB
HTML
34 lines
1.4 KiB
HTML
|
<p>An integer divisible by the <strong>sum</strong> of its digits is said to be a <strong>Harshad</strong> number. You are given an integer <code>x</code>. Return<em> the sum of the digits </em>of<em> </em><code>x</code><em> </em>if<em> </em><code>x</code><em> </em>is a <strong>Harshad</strong> number, otherwise, return<em> </em><code>-1</code><em>.</em></p>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong class="example">Example 1:</strong></p>
|
||
|
|
||
|
<div class="example-block">
|
||
|
<p><strong>Input:</strong> <span class="example-io">x = 18</span></p>
|
||
|
|
||
|
<p><strong>Output:</strong> <span class="example-io">9</span></p>
|
||
|
|
||
|
<p><strong>Explanation:</strong></p>
|
||
|
|
||
|
<p>The sum of digits of <code>x</code> is <code>9</code>. <code>18</code> is divisible by <code>9</code>. So <code>18</code> is a Harshad number and the answer is <code>9</code>.</p>
|
||
|
</div>
|
||
|
|
||
|
<p><strong class="example">Example 2:</strong></p>
|
||
|
|
||
|
<div class="example-block">
|
||
|
<p><strong>Input:</strong> <span class="example-io">x = 23</span></p>
|
||
|
|
||
|
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||
|
|
||
|
<p><strong>Explanation:</strong></p>
|
||
|
|
||
|
<p>The sum of digits of <code>x</code> is <code>5</code>. <code>23</code> is not divisible by <code>5</code>. So <code>23</code> is not a Harshad number and the answer is <code>-1</code>.</p>
|
||
|
</div>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong>Constraints:</strong></p>
|
||
|
|
||
|
<ul>
|
||
|
<li><code>1 <= x <= 100</code></li>
|
||
|
</ul>
|