C++實(shí)現(xiàn)LeetCode(202.快樂數(shù))
[LeetCode] 202.Happy Number 快樂數(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.
這道題定義了一種快樂數(shù),就是說對(duì)于某一個(gè)正整數(shù),如果對(duì)其各個(gè)位上的數(shù)字分別平方,然后再加起來得到一個(gè)新的數(shù)字,再進(jìn)行同樣的操作,如果最終結(jié)果變成了1,則說明是快樂數(shù),如果一直循環(huán)但不是1的話,就不是快樂數(shù),那么現(xiàn)在任意給我們一個(gè)正整數(shù),讓我們判斷這個(gè)數(shù)是不是快樂數(shù),題目中給的例子19是快樂數(shù),那么我們來看一個(gè)不是快樂數(shù)的情況,比如數(shù)字11有如下的計(jì)算過程:
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è)快樂數(shù),發(fā)現(xiàn)了規(guī)律后就要考慮怎么用代碼來實(shí)現(xiàn),我們可以用 HashSet 來記錄所有出現(xiàn)過的數(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 來做,我們并不需要太多的額外空間,關(guān)于非快樂數(shù)有個(gè)特點(diǎn),循環(huán)的數(shù)字中必定會(huì)有4,這里就不做證明了,我也不會(huì)證明,就是利用這個(gè)性質(zhì),就可以不用set了,參見代碼如下:
解法二:
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)一定存在,不過有的環(huán)不符合題意,只有最后 slow 停在了1的位置,才表明是一個(gè)快樂數(shù)。而且這里每次慢指針走一步,快指針走兩步,不是簡(jiǎn)單的指向next,而是要調(diào)用子函數(shù)計(jì)算各位上數(shù)字的平方和,當(dāng)快慢指針相等時(shí),跳出循環(huán),并且判斷慢指針是否為1即可,參見代碼如下:
解法三:
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;
}
};
類似題目:
參考資料:
https://leetcode.com/problems/happy-number/
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(202.快樂數(shù))的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)快樂數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)LeetCode(648.替換單詞)
- C++實(shí)現(xiàn)LeetCode(642.設(shè)計(jì)搜索自動(dòng)補(bǔ)全系統(tǒng))
- C++實(shí)現(xiàn)LeetCode(211.添加和查找單詞-數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì))
- C++實(shí)現(xiàn)LeetCode(208.實(shí)現(xiàn)字典樹(前綴樹))
- C++實(shí)現(xiàn)LeetCode(207.課程清單)
- C++實(shí)現(xiàn)LeetCode(237.刪除鏈表的節(jié)點(diǎn))
- C++實(shí)現(xiàn)LeetCode(203.移除鏈表元素)
- C++實(shí)現(xiàn)LeetCode(676.實(shí)現(xiàn)神奇字典)
相關(guān)文章
C/C++ winsock實(shí)現(xiàn)不同設(shè)備實(shí)時(shí)通訊的示例代碼
這篇文章主要為大家詳細(xì)介紹了C/C++如何利用winsock連接實(shí)現(xiàn)不同設(shè)備實(shí)時(shí)通訊,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09
Opencv?視頻讀取與寫入的實(shí)現(xiàn)示例
本文將介紹如何使用OpenCV進(jìn)行視頻讀寫。通過閱讀本文,您將了解如何讀取視頻文件、處理視頻幀、寫入視頻文件等操作,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
C語言三分鐘精通時(shí)間復(fù)雜度與空間復(fù)雜度
算法復(fù)雜度分為時(shí)間復(fù)雜度和空間復(fù)雜度。其作用:?時(shí)間復(fù)雜度是度量算法執(zhí)行的時(shí)間長(zhǎng)短;而空間復(fù)雜度是度量算法所需存儲(chǔ)空間的大小2022-02-02
C++中memcpy和memmove的區(qū)別總結(jié)
這篇文章主要介紹了C++中memcpy和memmove的區(qū)別總結(jié),這個(gè)問題經(jīng)常出現(xiàn)在C++的面試題目中,需要的朋友可以參考下2014-10-10
C語言中結(jié)構(gòu)體struct編寫的一些要點(diǎn)解析
這篇文章主要介紹了C語言中結(jié)構(gòu)體struct編寫的一些要點(diǎn)解析,談到了結(jié)構(gòu)體的聲明和指針指向等重要知識(shí)點(diǎn),需要的朋友可以參考下2016-04-04

