Happy Problem【leetcode202】

Tips:

第一次没有AC是因为没有判断1这个特殊的数,其他没有什么太难的地方,代码和注释如下:

public class Solution202 {public boolean isHappy(int n) {int nn = n,temp = 0;Set<Integer> set = new HashSet<Integer>();<span style="white-space:pre"></span>//用set检测是否回到了起点set.add(n);<span style="white-space:pre"></span>//将n加入if(n == 1)<span style="white-space:pre"></span>//1需要先判断return true;while(temp!=1) {temp = 0;while (nn != 0) {<span style="white-space:pre"></span>//各位的平方和temp += Math.pow(nn % 10, 2);nn = nn / 10;}if(set.contains(temp))<span style="white-space:pre"></span>return false;set.add(temp);nn = temp;}return true;<span style="white-space:pre"></span>//若循环结束则是Happy Number}}

,天不负;卧薪尝胆,三千越甲可吞吴。

Happy Problem【leetcode202】

相关文章:

你感兴趣的文章:

标签云: