Update 0202.快乐数.md 增加 Ruby 版本实现

This commit is contained in:
Lane Zhang (张健)
2024-10-14 10:00:24 +08:00
committed by GitHub
parent 42d84f8a7f
commit 19a6a4e369

View File

@ -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
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>