diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index 409a7471..39cb39fa 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -534,6 +534,30 @@ public class Solution { } ``` +### Ruby: + +```ruby +# @param {Integer} n +# @return {Boolean} +def is_happy(n) + @occurred_nums = Set.new + + while true + n = next_value(n) + + return true if n == 1 + + return false if @occurred_nums.include?(n) + + @occurred_nums << n + end +end + +def next_value(n) + n.to_s.chars.sum { |char| char.to_i ** 2 } +end +``` +