<span id="Label3"></p><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:‘Helvetica Neue‘,Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:‘Helvetica Neue‘,Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">Write an algorithm to determine if a number is "happy".</p></p><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:‘Helvetica Neue‘,Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:‘Helvetica Neue‘,Helvetica,Arial,sans-serif; font-size:14px; line-height:30px">A happy number is a number defined by the following process:starting with any positive integer and replace the number by the Sum of the squares of its digits, and repeat the process until the number equals 1 (where it would stay), or it loops Endl essly in a cycle which does not include 1. Those numbers for which this process ends in 1 is happy Numbers.</p></p><p style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:‘Helvetica Neue‘,Helvetica,Arial,sans-serif; font-size:14px; line-height:30px"><p style="margin-top:0px; margin-bottom:10px; Color:rgb (51,51,51); Font-family: ' Helvetica Neue ', helvetica,arial,sans-serif; font-size:14px; line-height:30px "><span style=" font-weight:700 ">example: </span> is a happy number </p></p> <ul style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:‘Helvetica Neue‘,Helvetica,Arial,sans-serif; font-size:14px; line-height:30px; list-style:none"> <ul style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:‘Helvetica Neue‘,Helvetica,Arial,sans-serif; font-size:14px; line-height:30px; list-style:none"> <li style="">1<span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em">2</span> + 9<span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em">2</span> = 82</li> <li style="">8<span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em">2</span> + 2<span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em">2</span> = 68</li> <li style="">6<span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em">2</span> + 8<span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em">2</span> = 100</li> </ul> </ul><p><p></p></p> <ul style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:‘Helvetica Neue‘,Helvetica,Arial,sans-serif; font-size:14px; line-height:30px; list-style:none"> <ul style="margin-top:0px; margin-bottom:10px; color:rgb(51,51,51); font-family:‘Helvetica Neue‘,Helvetica,Arial,sans-serif; font-size:14px; line-height:30px; list-style:none"> <li style="">1<span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em">2</span> + 0<span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em">2</span> + 0<span style="position:relative; font-size:12px; line-height:0; vertical-align:baseline; top:-0.5em">2</span> = 1</li> </ul> </ul><span style="font-family:Helvetica Neue,Helvetica,Arial,sans-serif; color:#333333"><span style="font-family:Helvetica Neue,Helvetica,Arial,sans-serif; color:#333333"><span style="font-size:14px; line-height:30px"></span></span></span><pre name="code" class="cpp"><pre name="code" class="cpp">Class solution {public: bool ishappy (int n) { map<int,int> value; While (n!=1) { value[n]=1; int remain; int tmp=0; While (n!=0) { remain=n%10; tmp+=remain*remain; n=n/10; } n=tmp; If (value.find (n)!=value.end ()) break;//has occurred } if (n!=1) return false; return true; }};</pre></pre><br><br><p><p></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Leetcode: Happy Number</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service