Leetcode刷题67-202. 快乐数(C++详细解法!!!)

题目来源:链接: [https://leetcode-cn.com/problems/perfect-squares/]

202. 快乐数

  • 1.问题描述
  • 2.我的解决方案
  • 3.大神们的解决方案
  • 4.我的收获

1.问题描述

编写一个算法来判断一个数是不是“快乐数”。

一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。
示例1:

输入: 19
输出: true
解释: 
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

2.我的解决方案

easy 类型题目。
这题的关键点就是:无限循环但始终变不到 1的数 如何退出循环。

AC代码:

class Solution {
   
public:
    bool isHappy(int n) {
   
        map<int, bool> res;
        int sum = 0;
        while(1)
        {
   
            int sum = 0; 
            while(n)
            {
   
                int tmp = n%10;
                sum += tmp*tmp;
                n /= 

你可能感兴趣的:(LeetCode从零开始,LeetCode,C++)