3099. Harshad Number Easy
1/**
2 * [3099] Harshad Number
3 *
4 * An integer divisible by the sum of its digits is said to be a Harshad number. You are given an integer x. Return the sum of the digits of x if x is a Harshad number, otherwise, return -1.
5 *
6 * <strong class="example">Example 1:
7 * <div class="example-block">
8 * Input: <span class="example-io">x = 18</span>
9 * Output: <span class="example-io">9</span>
10 * Explanation:
11 * The sum of digits of x is 9. 18 is divisible by 9. So 18 is a Harshad number and the answer is 9.
12 * </div>
13 * <strong class="example">Example 2:
14 * <div class="example-block">
15 * Input: <span class="example-io">x = 23</span>
16 * Output: <span class="example-io">-1</span>
17 * Explanation:
18 * The sum of digits of x is 5. 23 is not divisible by 5. So 23 is not a Harshad number and the answer is -1.
19 * </div>
20 *
21 * Constraints:
22 *
23 * 1 <= x <= 100
24 *
25 */
26pub struct Solution {}
27
28// problem: https://leetcode.com/problems/harshad-number/
29// discuss: https://leetcode.com/problems/harshad-number/discuss/?currentPage=1&orderBy=most_votes&query=
30
31// submission codes start here
32
33impl Solution {
34 pub fn sum_of_the_digits_of_harshad_number(x: i32) -> i32 {
35 0
36 }
37}
38
39// submission codes end
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn test_3099() {
47 }
48}
49
Back
© 2025 bowen.ge All Rights Reserved.