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" 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, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

解题思路:

第一个string为1,从第二个string开始,对前一个string的每一位进行操作,一位一位遍历,前后位相同,则记录当前数字数量,遇到前后不同,则对新的string进行写入。循环n-1次。

注意:

1、C++中string类型的正确操作;

2、C++中string和char*的区别和转换;

3、数字和字符Ascii码的转换方法;

特别注意:

代码中使用 n(int) + '0' 来表示'n',当且仅当n(int)<10时有效,因此如果sameDigCount超过10,也就是连续出现10个相同数字时,程序失败。不过可以轻松证明(证明略),连续出现的数字最多3个,也就是string串中最大的数字是3,不会超过4。因此代码是没有问题的。

 class Solution {
public:
string countAndSay(int n) {
string currentStr = ""; if (n<=)
return ""; while (--n) {
string nextStr = "";
int sameDigCount = ;
int strlen = currentStr.length();
char preDigital = currentStr[]; for (int i=; i<strlen; ++i) {
if (currentStr[i] == preDigital) {
++sameDigCount;
} else {
nextStr.push_back(sameDigCount+'');
nextStr.push_back(preDigital);
preDigital = currentStr[i];
sameDigCount = ;
}
} nextStr.push_back(sameDigCount+'');
nextStr.push_back(preDigital);
currentStr = nextStr;
} return currentStr;
}
};

附录:

C++ string 操作总结

【Leetcode】【Easy】Count and Say的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  6. 【leetcode刷题笔记】Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  7. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  8. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  9. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  10. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

随机推荐

  1. touch插件

    第一种: <script> (function($) { var options, Events, Touch; options = { x: 20, y: 20 }; Events = ...

  2. Python批量复制迁移文件夹

    前言 Python可以利用shutil库进行对文件夹,文件的迁移.而在本次的实践当中,难点在于目标文件夹的名称和数据源文件夹的名称,需要利用 工作单位提供的中间数据去进行对比连接起来. 例如:目标源的 ...

  3. C++中函数调用时的三种参数传递方式

    在C++中,参数传递的方式是“实虚结合”. 按值传递(pass by value) 地址传递(pass by pointer) 引用传递(pass by reference) 按值传递的过程为:首先计 ...

  4. Dictionary<string, object>不区分大小写

    Dictionary<string, object> dic = new Dictionary<string, object>(StringComparer.OrdinalIg ...

  5. <数据挖掘导论>读书笔记4--其他分类技术

    1.基于规则的分类器 2.最近邻分类器 3.贝叶斯分类器 4.人工神经网络 5.支持向量机 6.组合方法 7.不平衡类问题 8.多类问题

  6. ORM框架SQLAlchemy的使用

    ORM和SQLAlchemy简介 对象关系映射(Object Relational Mapping,简称ORM),简单的来说,ORM是将数据库中的表与面向对象语言中的类建立了一种对应的关系.然后我们操 ...

  7. spring mvc如何优雅的使用fastjson

    1. 在spring mvc中配置fastjson <!-- 设置配置方案 --> <mvc:annotation-driven> <!-- 设置不使用默认的消息转换器 ...

  8. 05.部分类 partial

    namespace _06.部分类 { class Program { static void Main(string[] args) { } } /// <summary> /// 这个 ...

  9. 九、cent OS下tomcat改变日志目录

    修改catalina.out的目录bin目录下修改catalina.sh:CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out  此行内的 ...

  10. POJ 2955 Brackets 区间DP 最大括号匹配

    http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...