202. Happy Number Easy

@problem@discussion
#Hash Table#Math#Two Pointers



1/**
2 * [202] Happy Number
3 *
4 * Write an algorithm to determine if a number n is happy.
5 * A happy number is a number defined by the following process:
6 * 
7 * 	Starting with any positive integer, replace the number by the sum of the squares of its digits.
8 * 	Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
9 * 	Those numbers for which this process ends in 1 are happy.
10 * 
11 * Return true if n is a happy number, and false if not.
12 *  
13 * Example 1:
14 * 
15 * Input: n = 19
16 * Output: true
17 * Explanation:
18 * 1^2 + 9^2 = 82
19 * 8^2 + 2^2 = 68
20 * 6^2 + 8^2 = 100
21 * 1^2 + 0^2 + 0^2 = 1
22 * 
23 * Example 2:
24 * 
25 * Input: n = 2
26 * Output: false
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	1 <= n <= 2^31 - 1
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/happy-number/
37// discuss: https://leetcode.com/problems/happy-number/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42    pub fn is_happy(n: i32) -> bool {
43        false
44    }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_202() {
55    }
56}
57


Back
© 2025 bowen.ge All Rights Reserved.