【双指针】Leetcode 202.快乐数-代码编写

class Solution {
public:
//相当于指针往后面移动一步
    int BitSum(int num)
    {
        int ret = 0;
        while(num)
        {
            ret += pow(num % 10, 2);
            num /= 10;
        }
        return ret;
    }

    bool isHappy(int n) {
        int slow = n;
        int fast = BitSum(n);
        while(fast != slow)
        {
            slow = BitSum(slow);
            fast = BitSum(BitSum(fast));
        }
        //走到这里,说明一定是相遇了,判断就是了
        if(fast == 1)return true;
        return false;
    }
};

上一篇:ElasticSearch文档批量操作[ES系列] - 第


下一篇:2024.3.25