欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)LeetCode(202.快樂(lè)數(shù))

 更新時(shí)間:2021年08月06日 15:21:28   作者:Grandyang  
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(202.快樂(lè)數(shù)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

[LeetCode] 202.Happy Number 快樂(lè)數(shù)

Write an algorithm to determine if a number is "happy".

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 

Input: 19
Output: true
Explanation:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1

Credits:
Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.

這道題定義了一種快樂(lè)數(shù),就是說(shuō)對(duì)于某一個(gè)正整數(shù),如果對(duì)其各個(gè)位上的數(shù)字分別平方,然后再加起來(lái)得到一個(gè)新的數(shù)字,再進(jìn)行同樣的操作,如果最終結(jié)果變成了1,則說(shuō)明是快樂(lè)數(shù),如果一直循環(huán)但不是1的話,就不是快樂(lè)數(shù),那么現(xiàn)在任意給我們一個(gè)正整數(shù),讓我們判斷這個(gè)數(shù)是不是快樂(lè)數(shù),題目中給的例子19是快樂(lè)數(shù),那么我們來(lái)看一個(gè)不是快樂(lè)數(shù)的情況,比如數(shù)字11有如下的計(jì)算過(guò)程:

1^2 + 1^2 = 2
2^2 = 4
4^2 = 16
1^2 + 6^2 = 37
3^2 + 7^2 = 58
5^2 + 8^2 = 89
8^2 + 9^2 = 145
1^2 + 4^2 + 5^2 = 42
4^2 + 2^2 = 20
2^2 + 0^2 = 4

我們發(fā)現(xiàn)在算到最后時(shí)數(shù)字4又出現(xiàn)了,那么之后的數(shù)字又都會(huì)重復(fù)之前的順序,這個(gè)循環(huán)中不包含1,那么數(shù)字11不是一個(gè)快樂(lè)數(shù),發(fā)現(xiàn)了規(guī)律后就要考慮怎么用代碼來(lái)實(shí)現(xiàn),我們可以用 HashSet 來(lái)記錄所有出現(xiàn)過(guò)的數(shù)字,然后每出現(xiàn)一個(gè)新數(shù)字,在 HashSet 中查找看是否存在,若不存在則加入表中,若存在則跳出循環(huán),并且判斷此數(shù)是否為1,若為1返回true,不為1返回false,代碼如下:

解法一:

class Solution {
public:
    bool isHappy(int n) {
        unordered_set<int> st;
        while (n != 1) {
            int sum = 0;
            while (n) {
                sum += (n % 10) * (n % 10);
                n /= 10;
            }
            n = sum;
            if (st.count(n)) break;
            st.insert(n);
        }
        return n == 1;
    }
};

其實(shí)這道題也可以不用 HashSet 來(lái)做,我們并不需要太多的額外空間,關(guān)于非快樂(lè)數(shù)有個(gè)特點(diǎn),循環(huán)的數(shù)字中必定會(huì)有4,這里就不做證明了,我也不會(huì)證明,就是利用這個(gè)性質(zhì),就可以不用set了,參見(jiàn)代碼如下:

解法二:

class Solution {
public:
    bool isHappy(int n) {
        while (n != 1 && n != 4) {
            int sum = 0;
            while (n) {
                sum += (n % 10) * (n % 10);
                n /= 10;
            }
            n = sum;
        }
        return n == 1;
    }
};

這道題還有一種快慢指針的解法,由熱心網(wǎng)友喵團(tuán)團(tuán)提供,跟之前那道 Linked List Cycle 檢測(cè)環(huán)的方法類似,不同的是這道題環(huán)一定存在,不過(guò)有的環(huán)不符合題意,只有最后 slow 停在了1的位置,才表明是一個(gè)快樂(lè)數(shù)。而且這里每次慢指針走一步,快指針走兩步,不是簡(jiǎn)單的指向next,而是要調(diào)用子函數(shù)計(jì)算各位上數(shù)字的平方和,當(dāng)快慢指針相等時(shí),跳出循環(huán),并且判斷慢指針是否為1即可,參見(jiàn)代碼如下:

解法三:

class Solution {
public:
    bool isHappy(int n) {
        int slow = n, fast = n;
        while (true) {
            slow = findNext(slow);
            fast = findNext(fast);
            fast = findNext(fast);
            if (slow == fast) break;
        }
        return slow == 1;
    }
    int findNext(int n) {
        int res = 0;
        while (n > 0) {
            res += (n % 10) * (n % 10);
            n /= 10;
        }
        return res;
    }
};

類似題目:

Linked List Cycle

參考資料:

https://leetcode.com/problems/happy-number/

https://leetcode.com/problems/happy-number/discuss/56913/Beat-90-Fast-Easy-Understand-Java-Solution-with-Brief-Explanation

https://leetcode.com/problems/happy-number/discuss/56917/My-solution-in-C(-O(1)-space-and-no-magic-math-property-involved-)

到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(202.快樂(lè)數(shù))的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)快樂(lè)數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論