力扣202-快乐数

快乐数

题目链接

解题思路:

  1. 两个指针,一快一慢,如果相遇,就会生成环
  2. 如果环内元素为1,那么就可以返回
class Solution {
   
public:
    int get(int n){
   
        int res = 0;
        while(n){
   
            res += (n%10) * (n%10);
            n /= 10;
        }
        return res;
    }
    bool isHappy(int n) {
   
        int f = get(n);
        int s = n;
        while( f != s ){
   
            f = get(get(f));
            s = get(s);
        }
        return f==1;
    }
};

相关推荐

  1. 202-快乐

    2024-02-03 19:04:03       37 阅读
  2. 202题“快乐

    2024-02-03 19:04:03       6 阅读
  3. 【LeetCode】202. 快乐

    2024-02-03 19:04:03       34 阅读
  4. leetcode 202. 快乐

    2024-02-03 19:04:03       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-03 19:04:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-03 19:04:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-03 19:04:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-03 19:04:03       18 阅读

热门阅读

  1. 力扣反转两次的数字

    2024-02-03 19:04:03       28 阅读
  2. leetcode-用栈实现队列

    2024-02-03 19:04:03       29 阅读
  3. Unity DOTween插件常用方法(一)

    2024-02-03 19:04:03       29 阅读
  4. MySQL进阶之触发器

    2024-02-03 19:04:03       26 阅读
  5. mysql b+搜索的算法次数的计算

    2024-02-03 19:04:03       29 阅读
  6. vscode 突然连接不上服务器了

    2024-02-03 19:04:03       30 阅读
  7. 积分、权益、卡卷 三者的理解

    2024-02-03 19:04:03       30 阅读
  8. 如何用Pycharm在本地调用chatgpt的接口

    2024-02-03 19:04:03       32 阅读
  9. eCos flash模拟EEPROM实现NV系统

    2024-02-03 19:04:03       27 阅读