|
| 1 | +<h2><a href="https://leetcode.com/problems/alternating-digit-sum/">2544. Alternating Digit Sum</a></h2><h3>Easy</h3><hr><p>You are given a positive integer <code>n</code>. Each digit of <code>n</code> has a sign according to the following rules:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li>The <strong>most significant digit</strong> is assigned a <strong>positive</strong> sign.</li> |
| 5 | + <li>Each other digit has an opposite sign to its adjacent digits.</li> |
| 6 | +</ul> |
| 7 | + |
| 8 | +<p>Return <em>the sum of all digits with their corresponding sign</em>.</p> |
| 9 | + |
| 10 | +<p> </p> |
| 11 | +<p><strong class="example">Example 1:</strong></p> |
| 12 | + |
| 13 | +<pre> |
| 14 | +<strong>Input:</strong> n = 521 |
| 15 | +<strong>Output:</strong> 4 |
| 16 | +<strong>Explanation:</strong> (+5) + (-2) + (+1) = 4. |
| 17 | +</pre> |
| 18 | + |
| 19 | +<p><strong class="example">Example 2:</strong></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | +<strong>Input:</strong> n = 111 |
| 23 | +<strong>Output:</strong> 1 |
| 24 | +<strong>Explanation:</strong> (+1) + (-1) + (+1) = 1. |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><strong class="example">Example 3:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> n = 886996 |
| 31 | +<strong>Output:</strong> 0 |
| 32 | +<strong>Explanation:</strong> (+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0. |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p> </p> |
| 36 | +<p><strong>Constraints:</strong></p> |
| 37 | + |
| 38 | +<ul> |
| 39 | + <li><code>1 <= n <= 10<sup>9</sup></code></li> |
| 40 | +</ul> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | +<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0; |
| 44 | +} |
| 45 | +.spoiler {overflow:hidden;} |
| 46 | +.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;} |
| 47 | +.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;} |
| 48 | +.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;} |
| 49 | +</style> |
0 commit comments