题目:

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.

代码:

class Solution {
public:
string countAndSay(int n) {
string tmp1 = "";
string tmp2 = "";
for ( size_t i = ; i < n; ++i )
{
int digit_count = ;
for ( size_t j = ; j < tmp1.size(); ++j )
{
if ( tmp1[j]==tmp1[j-] )
{
++digit_count;
}
else
{
tmp2 += digit_count+'';
tmp2 += tmp1[j-];
digit_count = ;
}
}
tmp2 += digit_count+'';
tmp2 += tmp1[tmp1.size()-];
tmp1 = tmp2;
tmp2 = "";
}
return tmp1;
}
};

tips:

这个题意不太好理解。

简单说就是:第n组字符串是第n-1组字符串的读法;读法的准则就是‘连续出现个数+数字’。

其余的就是处理一下边界case。

=======================================

第二次过这道题,对题意理解好了之后,代码一次AC。

class Solution {
public:
string countAndSay(int n) {
if (n<) return "";
string ret = "";
for ( int i=; i<n; ++i )
{
string tmp_ret = "";
int count_same_digit = ;
char curr_digit = ret[];
for ( int j=; j<ret.size(); ++j )
{
if ( ret[j]!=ret[j-] )
{
tmp_ret += string(,count_same_digit+'') + string(,curr_digit);
curr_digit = ret[j];
count_same_digit = ;
}
else
{
count_same_digit++;
}
}
tmp_ret += string(,count_same_digit+'') + string(,curr_digit);
ret = tmp_ret;
}
return ret;
}
};

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

  1. 【Integer To Roman】cpp

    题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  2. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

  3. 【Single Num II】cpp

    题目: Given an array of integers, every element appears three times except for one. Find that single o ...

  4. hdu 3336【Count the string】(KMP)

    一道字符串匹配的题目,仅仅借此题练习一下KMP 因为这道题目就是要求用从头开始的n个字符串去匹配原来的字符串,很明显与KMP中求next的过程很相似,所以只要把能够从头开始匹配一定个数的字符串的个数加 ...

  5. 【Search Insert Position 】cpp

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

  6. 【First Missing Positive】cpp

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  7. 【Insertion Sorted List】cpp

    题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...

  8. 【Merge Sorted Array】cpp

    题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...

  9. 【Path Sum II】cpp

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

随机推荐

  1. C puzzles详解【6-8题】

    第六题 #include<stdio.h> int main() { ; switch(a) { ': printf("ONE\n"); break; ': print ...

  2. IOS基础——IOS学习路线图(一)

    一.一个月 1.OC语法基础. 2.KVC和KVO 3.IOS UI基础 4.UI表视图与集合视图 5.UIStoryboard和autoLayout 6.Ipad API 二.10天 7.静态页面考 ...

  3. Show Users Assigned to a Specific Role

    In a previous post I showed you how to know what Roles are assigned to a specific user. But here is ...

  4. Show Roles Assigned to a Specific User

     Here is a query that I often use to lookup Roles assigned to a specific PeopleSoft user. At run tim ...

  5. C#中如何将combox中的下拉项和一个枚举中的各项进行绑定

    实现一个combobox,将其各个下拉项与枚举进行绑定 效果图如下: 代码详解如下: 枚举: public enum StoreSite { /// <summary> /// 未知 // ...

  6. Elipse安装Spring Tool Suite

    STS实际上是对Eclipse的Spring包装,下载STS IDE可以直接开发spring web. 但大多数人还是喜欢使用eclipse.下面就eclipse安装sts插件做个介绍. 1.首先到s ...

  7. Silverlight Color的颜色值

    1.MainPage.xaml <UserControl xmlns:SysManage="clr-namespace:Application" x:Class=" ...

  8. 三、MongoDB的创建、更新和删除

    一.MongoDB的下载.安装与部署 二.MongoDB的基础知识简介 三.MongoDB的创建.更新和删除 概要 下面开始学习MongoDB最重要也是最基础的部分:C(创建)R(查询)U(更新)D( ...

  9. 使用MySQL数据库

    登录到MySQL 当 MySQL 服务已经运行时, 我们可以通过MySQL自带的客户端工具登录到MySQL数据库中, 首先打开命令提示符, 输入以下格式的命名: mysql -h 主机名 -u 用户名 ...

  10. MongoDB的主要特性概述

    一.文档数据模型 文档是一组属性名和属性的集合.相较于关系数据库复杂的规范化,面向文档的数据模型很容易以聚合的形式来表示数据.文档采用无Schema的形式,这种做法带来了一定的优势:首先,由应用程序, ...