Class Solution

java.lang.Object
g2501_2600.s2544_alternating_digit_sum.Solution

public class Solution extends Object
2544 - Alternating Digit Sum.<p>Easy</p> <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> <ul> <li>The <strong>most significant digit</strong> is assigned a <strong>positive</strong> sign.</li> <li>Each other digit has an opposite sign to its adjacent digits.</li> </ul> <p>Return <em>the sum of all digits with their corresponding sign</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 521</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> (+5) + (-2) + (+1) = 4.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 111</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> (+1) + (-1) + (+1) = 1.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> n = 886996</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> (+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • alternateDigitSum

      public int alternateDigitSum(int n)