C++實(shí)現(xiàn)LeetCode(38.計(jì)數(shù)和讀法)
[LeetCode] 38. Count and Say 計(jì)數(shù)和讀法
The count-and-say sequence is the sequence of integers with the first five terms as following:
1. 1
2. 11
3. 21
4. 1211
5. 111221
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
Input: 1
Output: "1"
Example 2:
Input: 4
Output: "1211"
這道計(jì)數(shù)和讀法問(wèn)題還是第一次遇到,看似挺復(fù)雜,其實(shí)仔細(xì)一看,算法很簡(jiǎn)單,就是對(duì)于前一個(gè)數(shù),找出相同元素的個(gè)數(shù),把個(gè)數(shù)和該元素存到新的 string 里。代碼如下:
class Solution {
public:
string countAndSay(int n) {
if (n <= 0) return "";
string res = "1";
while (--n) {
string cur = "";
for (int i = 0; i < res.size(); ++i) {
int cnt = 1;
while (i + 1 < res.size() && res[i] == res[i + 1]) {
++cnt;
++i;
}
cur += to_string(cnt) + res[i];
}
res = cur;
}
return res;
}
};
打印出了前 12 個(gè)數(shù)字,發(fā)現(xiàn)一個(gè)很有意思的現(xiàn)象,不管打印到后面多少位,出現(xiàn)的數(shù)字只是由 1, 2 和3 組成,前十二個(gè)數(shù)字如下:
1
1 1
2 1
1 2 1 1
1 1 1 2 2 1
3 1 2 2 1 1
1 3 1 1 2 2 2 1
1 1 1 3 2 1 3 2 1 1
3 1 1 3 1 2 1 1 1 3 1 2 2 1
1 3 2 1 1 3 1 1 1 2 3 1 1 3 1 1 2 2 1 1
1 1 1 3 1 2 2 1 1 3 3 1 1 2 1 3 2 1 1 3 2 1 2 2 2 1
3 1 1 3 1 1 2 2 2 1 2 3 2 1 1 2 1 1 1 3 1 2 2 1 1 3 1 2 1 1 3 2 1 1
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(38.計(jì)數(shù)和讀法)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)計(jì)數(shù)和讀法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)LeetCode(94.二叉樹(shù)的中序遍歷)
- C++實(shí)現(xiàn)LeetCode(112.二叉樹(shù)的路徑和)
- C++實(shí)現(xiàn)LeetCode(78.子集合)
- C++實(shí)現(xiàn)LeetCode(47.全排列之二)
- C++實(shí)現(xiàn)LeetCode(90.子集合之二)
- C++實(shí)現(xiàn)LeetCode(113.二叉樹(shù)路徑之和之二)
- C++實(shí)現(xiàn)LeetCode(39.組合之和)
- C++實(shí)現(xiàn)LeetCode(51.N皇后問(wèn)題)
- C++實(shí)現(xiàn)LeetCode(77.Combinations 組合項(xiàng))
- C++實(shí)現(xiàn)LeetCode(46.全排列)
- C++實(shí)現(xiàn)LeetCode(43.字符串相乘)
相關(guān)文章
C++紅黑樹(shù)的底層實(shí)現(xiàn)機(jī)制詳解
紅黑樹(shù)與AVL樹(shù)一樣,也是一種自平衡的二叉搜索樹(shù),它在每個(gè)結(jié)點(diǎn)上增加一個(gè)存儲(chǔ)位表示結(jié)點(diǎn)的顏色,可以是Red或Black,通過(guò)對(duì)任何一條從根到葉子的路徑上各個(gè)結(jié)點(diǎn)著色方式的限制,本文介紹了C++紅黑樹(shù)的底層實(shí)現(xiàn)機(jī)制,需要的朋友可以參考下2024-08-08
C++ 中重載和運(yùn)算符重載加號(hào)實(shí)現(xiàn)矩陣相加實(shí)例代碼
這篇文章主要介紹了C++ 中重載和運(yùn)算符重載加號(hào)實(shí)現(xiàn)矩陣相加實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
OpenCV基于背景減除實(shí)現(xiàn)行人計(jì)數(shù)
本文主要介紹了如何使用OpenCV C++對(duì)視頻中的人流量進(jìn)行統(tǒng)計(jì)。文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)OpenCV有一定的幫助,需要的可以了解一下2022-01-01
C/C++?Qt?TabWidget?實(shí)現(xiàn)多窗體創(chuàng)建詳解
TabWidget組件配合自定義Dialog組件,可實(shí)現(xiàn)一個(gè)復(fù)雜的多窗體分頁(yè)結(jié)構(gòu)。這篇文章就主要介紹了如何通過(guò)TabWidget實(shí)現(xiàn)多窗體的創(chuàng)建,感興趣的小伙伴可以了解一下2021-12-12
C 語(yǔ)言基礎(chǔ)----詳解C中的運(yùn)算符
這篇文章主要介紹了C語(yǔ)言中的運(yùn)算符,文中講解非常詳細(xì),適合初學(xué)小白進(jìn)行學(xué)習(xí),想入門(mén)C語(yǔ)言的朋友不妨了解下2020-06-06
C++利用多態(tài)實(shí)現(xiàn)職工管理系統(tǒng)(項(xiàng)目開(kāi)發(fā))
這篇文章主要介紹了C++利用多態(tài)實(shí)現(xiàn)職工管理系統(tǒng)(項(xiàng)目開(kāi)發(fā)),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01

