count-and-say 計數和發言
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1is read off as"one 1"or11.
11is read off as"two 1s"or21.
21is read off as"one 2, thenone 1"or1211.
Given an integer n, generate the n th sequence.
Note: The sequence of integers will be represented as a string.
題意是n=1時輸出字串1;n=2時,數上次字串中的數值個數,因為上次字串有1個1,所以輸出11;n=3時,由於上次字元是11,有2個1,所以輸出21;n=4時,由於上次字串是21,有1個2和1個1,所以輸出1211。依次類推,寫個countAndSay(n)函式返回字串。
典型的遞迴問題。
實現程式碼: class Solution {
public:
string countAndSay(int n) {
if(n == 1) return "1";
//遞迴呼叫,然後對字串處理
string str = countAndSay(n-1) + "*";//為了str末尾的標記,方便迴圈讀數
int count = 1; //用於計算每個字元出現的次數;
string s = "";
for(int i = 0; i < str.length() - 1;i++) //因為增加了一個“*”,所以只能遍歷到"*"之前的字元;
{
if(str[i] == str[i+1])
{
count++;//計數增加
}
else
{
s = s + to_string(count) + str[i];//個數在前,字元在後;
count = 1;//初始化,為後面的字元做準備;
}
}
return s;
}
};
相關推薦
leetCode 38.Count and Say (計數和發言) 解題思路和方法
Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as
count-and-say 計數和發言
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1is read off as"one 1"or11. 11is read off as"t
[Leetcode] count and say 計數和說
str 一個 實現 當前 ger repr begin lee http The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221,
[LeetCode] 38. Count and Say 計數和讀法
相同 output for ews represent class urn present count The count-and-say sequence is the sequence of integers with the first five terms as f
38. Count and Say
integer ive output while each int result first seq The count-and-say sequence is the sequence of integers with the first five terms as f
leetcode-38 Count And Say
pop -m seq rac note 不變 article ger else 問題描寫敘述: The count-and-say sequence is the sequence of integersbeginning as follows: 1, 11,
LeetCode 38: Count and Say
log mat rate and style valueof dsa blog res class Solution { public String countAndSay(int n) { if (n == 0) { re
LeeCode from 0 ——38. Count and Say
urn ++ temp ont RM fir IT The family 38. Count and Say The count-and-say sequence is the sequence of integers with the first five terms a
38. Count and Say 題目解釋
註意 ron 輸出 n-1 tail .net -a 字符 描述 初看題目很迷,Google看了各大神的解釋,才明白 原題連接:https://leetcode.com/problems/count-and-say/description/ 題目解釋:原題的意思就是用一個
【算法】LeetCode算法題-Count And Say
對象 nds iou 得到 eas for 結果 支持 amp 這是悅樂書的第153次更新,第155篇原創 01 看題和準備 今天介紹的是LeetCode算法題中Easy級別的第12題(順位題號是38)。count-and-say序列是整數序列,前五個術語如下: 1 11
LeetCode-38. Count and Say
0.原題 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.
LeetCode Day31 count-and-say
class Solution { public: string countAndSay(int n) { if (n <= 0) return ""; string res = "1"; while (--n) {
【演算法】LeetCode演算法題-Count And Say
這是悅樂書的第153次更新,第155篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第12題(順位題號是38)。count-and-say序列是整數序列,前五個術語如下: 1 11 21 1211
LeetCode38:Count and Say
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. 11122
【leetcode】38. Count and Say
題目描述 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.
[Leetcode38]Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. &nbs
[leetcode]38. Count and Say
健身完回來狀態不好,先做個easy吧 這個破題的decription有歧義吧,簡直了。 Solution 1: 沒什麼技術含量的遍歷 每執行一個count的時間複雜度是m(就是這一遍的長度),一共執行了n次這樣的count,m又是一個增長的量。但是我不知道m的增長速率。我覺得時間複雜
LeetCode--38. Count and Say
今早心血來潮想找一條easy題刷一下,就找了個字串的題目:https://leetcode.com/problems/count-and-say/ 這個題目雖然是easy題目,但題意理解起來需要一會兒呢。
leetcode 38. Count and Say
題目解釋有點繞:大致意思是 1 可以讀作 1個1(11) 11可以讀作2個1(21).。。。 明白題意後,可以想到,遍歷字串,計數重複的數字,連線字串即可 class Solution { public: string countAndSay(int n
Count and Say
/* * 題目的關鍵就是搞懂題意,數上個n對應的數字: * 當n=1結果是 1; * 當n=2結果是 1個1,結果就是21; * 當n=3結果是 1個2,1個1,結果就是1211; * 當n=4結果是 1個1,1個2,