482.Check if a Number Is an Armstrong Number
Easy
An Armstrong number equals the sum of its own digits, each raised to the power of how many digits it has (153 = 1³ + 5³ + 3³). Given a non-negative integer
n, return whether it's an Armstrong number.
Both solutions need to know the digit count before they can raise anything to the right power. The two-passTwo-Pass CountingLooping once just to count the digits, then looping a second time (over a fresh copy of n) to actually sum the powers. version finds that count the straightforward way — by looping. The string-lengthString Length ShortcutReading the digit count directly from the length of n's string form, leaving only the power-sum loop to write. version gets the same count in one step, the same trick used in Count the Number of Digits in an Integer.
Example 1:
Input: n = 153
Output: true
Example 2:
Input: n = 123
Output: false
Example 3:
Input: n = 9474
Output: true
+ 5 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ n ≤ 999999
n =
153