Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ddf0b5

Browse files
Merge pull request youngyangyang04#2787 from gazeldx/master
Update 0202.快乐数.md 增加 Ruby 版本实现
2 parents 124c8db + 19a6a4e commit 8ddf0b5

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎problems/0202.快乐数.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,30 @@ public class Solution {
534534
}
535535
```
536536

537+
### Ruby:
538+
539+
```ruby
540+
# @param {Integer} n
541+
# @return {Boolean}
542+
def is_happy(n)
543+
@occurred_nums = Set.new
544+
545+
while true
546+
n = next_value(n)
547+
548+
return true if n == 1
549+
550+
return false if @occurred_nums.include?(n)
551+
552+
@occurred_nums << n
553+
end
554+
end
555+
556+
def next_value(n)
557+
n.to_s.chars.sum { |char| char.to_i ** 2 }
558+
end
559+
```
560+
537561
<p align="center">
538562
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
539563
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /