class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @return bool布尔型
     */
    bool happynum(int n) {
        // write code here
        while(1){
            string s=to_string(n);
            if(s.length()==1){//当数字只有个位时,判断是否为1
                if(n==1)return true;
                else return false;
            }
            int temp=0;
            while(n){
                int a=n%10;
                temp+=a*a;
                n/=10;
            }
            n=temp;
            temp=0;
        }
        
    }
};

这道题好像不用哈希也可以做,我是用的字符串。