【题目】
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1
is read off as"one
1"
or11
.
11
is read off as"two
1s"
or21
.
21
is read off as"one
2
, thenone 1"
or1211
.
Given an integern, generate thenthsequence.
Note: The sequence of integers will be represented as a string.
【分析】
111221 统计相邻重复的字符个数,例如这里111有3个1重复相邻,输出31,22有两个2重复相邻,输出22,1只有一个1,输出11
【代码】
/*********************************
* 日期:2015-01-27
* 作者:SJF0115
* 题目: 38.Count and Say
* 网址:https://oj.leetcode.com/problems/count-and-say/
* 结果:AC
* 来源:LeetCode
* 博客:
**********************************/
#include <iostream>
using namespace std;
class Solution {
public:
string countAndSay(int n) {
if(n <= 0){
return "";
}//if
string str("1");
for(int i = 1;i < n;++i){
NextCountAndSay(str);
}//for
return str;
}
private:
void NextCountAndSay(string& str){
int len = str.length();
string tmp = "";
for(int i = 0;i < len;++i){
int repeatCount = 1;
// repeat char count
while((i+1 < len) && (str[i] == str[i+1])){
++repeatCount;
++i;
}//
tmp += to_string(repeatCount);
tmp += str[i];
}//for
str = tmp;
}
};
int main(){
Solution solution;
int n = 5;
string result = solution.countAndSay(n);
// 输出
cout<<result<<endl;
return 0;
}
分享到:
相关推荐
Leetcode原题Count and Say count-and-say序列是整数序列,前五个术语如下: 1. 1 2. 11 3. 21 1211 5. 111221 1被读作“1”或11。 11被读作“两个1”或21。 21被读作“一个2,然后一个1”或1211。 给定整数n,...
201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](./C++/bitwise-and-of-numbers-range.cpp) [Python](./Python/bitwise-and-of-numbers-range.py) | _...
https://leetcode.com/problems/count-and-say/ 3, java/dungenegame https://leetcode.com/problems/dungeon-game/ 4, java/findminrotaed 5, java/houserobber https://leetcode.com/problems/house-robber/
Practice-Leetcode 这是一个Chinese School Girl:China:用来练习leetcode的文档.每道下面的题都有详细的解题思路,和知识点分析,尽请参考。 找实习的小伙伴也可以参考我的Tech-blog...038.Count and Say 递归 040.C
leetcode 答案LeetCode LeetCode in Swift 这个Repo 用来存下我在LeetCode ...Count and Say Easy #53 Maximum Subarray Easy #66 Plus One Easy #70 Climbing Stairs Easy #83 Remove Duplicates from Sorted L
1_count_and_say.cpp - super_ugly_number.cpp - Detect_Pattern.cpp - degree_of_array.cpp - 键盘.cpp - 2Sum_Data_Structure_Design.cpp - shuffle_array.cpp - permutations.cpp - kth_missing_number.cpp - 3...
3.12 Count and Say 3.13 变位词 3.14 简化系统路径 3.15 最后一个单词的长度 3.16 反转字符串中的单词 3.16.1 字符串前后和中间可能存在多个空格 3.16.2 不存在前后和中间的多余空格 3.17 一个编辑距离 4. 栈 4.1 ...
leetcode中国 我自己的leetcode刷题记录 ###[20150920] ...count and say , easy , 模拟 Anagrams 字符串处理,map Simplify Path,字符串处理,stack Length of Last Word,字符串处理.细节题 Rever
leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 备注 0008 String to Integer(atoi) ...Count and Say :star: 0043 Multiply Strings :star: :star: 大数相乘 0044 Wild Card Matchi
Count_and_Say 循环n次依次计算即可,注意0次等边界情况 Longest_Consecutive_Sequence 双向确认是否连续,避免重复的n^2复杂度 Palindrome_Partitioning DP,避免递归调用,利用数组储存已算出来的数值,由后向前...
leetcode给单链表加一js实现 LeetCode By JavaScript LeetCode Solutions (All By JavaScript!) 的个人Solutions汇总,全部使用JavaScript完成:grinning_squinting_face: 目的:了解、掌握数据结构和算法,当然...Say:
“count_n_say.py” - 使用迭代方法生成 Count 和 Say 序列的第 n 项。 “count_segments.py” - 该算法计算字符串中的段数,其中段被定义为连续的非空格字符序列。 "count_negatives_2d_array.py" - 给定一个二维...
Count and Say Anagrams Valid Anagram Simplify Path Length of Last Word Isomorphic Strings Word Pattern 栈和队列 栈 Min Stack Valid Parentheses Longest Valid Parentheses Largest Rectangle in Histogram ...