Leetcode 202. Happy Number

来源:互联网 发布:舆情监测软件价格 编辑:程序博客网 时间:2024/05/23 14:52
public class Solution {    public boolean isHappy(int n) {        HashSet<Integer> hs = new HashSet<Integer>();        // if repeat, HashSet.add() will return false        while (hs.add(n)) {            // sum the squares of n's digits            int sum = 0;            while (n > 0) {                sum += (n % 10)*(n % 10);                n /= 10;            }            if (sum == 1) return true;            n = sum;        }        return false;    }}

0 0
原创粉丝点击