258. Add Digits Easy

@problem@discussion
#Math#Simulation#Number Theory



1/**
2 * [258] Add Digits
3 *
4 * Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.
5 *  
6 * Example 1:
7 * 
8 * Input: num = 38
9 * Output: 2
10 * Explanation: The process is
11 * 38 --> 3 + 8 --> 11
12 * 11 --> 1 + 1 --> 2 
13 * Since 2 has only one digit, return it.
14 * 
15 * Example 2:
16 * 
17 * Input: num = 0
18 * Output: 0
19 * 
20 *  
21 * Constraints:
22 * 
23 * 	0 <= num <= 2^31 - 1
24 * 
25 *  
26 * Follow up: Could you do it without any loop/recursion in O(1) runtime?
27 * 
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/add-digits/
32// discuss: https://leetcode.com/problems/add-digits/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36impl Solution {
37    pub fn add_digits(num: i32) -> i32 {
38        0
39    }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_258() {
50    }
51}
52


Back
© 2025 bowen.ge All Rights Reserved.