题目链接

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.


题目描述的不是很清楚,其实就是第i+1个字符串是第i个字符串的读法,第一字符串为 “1”

比如第四个字符串是1211,它的读法是 1个1、1个2,2个1,因此第五个字符串是111221。

第五个字符串的读法是:3个1、2个2、1个1,因此第六个字符串是312211                  本文地址

......

简单的模拟就可以。

 class Solution {
public:
string countAndSay(int n) {
if(n < )return "";
string prev = "";
for(int i = ; i <= n; i++)
{
char curChar = prev[];
int times = ;//curChar 出现的次数
string tmpstr;
prev.push_back('#');//处理边界条件
for(int k = ; k < prev.size(); k++)
{
if(prev[k] == curChar)
times++;
else
{
tmpstr += to_string(times);
tmpstr.push_back(curChar);
curChar = prev[k];
times = ;
}
}
prev = tmpstr;
}
return prev;
}
};

其实我们可以发现字符串中永远只会出现1,2,3这三个字符,假设第k个字符串中出现了4,那么第k-1个字符串必定有四个相同的字符连续出现,假设这个字符为1,则第k-1个字符串为x1111y。第k-1个字符串是第k-2个字符串的读法,即第k-2个字符串可以读为“x个1,1个1,1个y” 或者“*个x,1个1,1个1,y个*”,这两种读法分别可以合并成“x+1个1,1个y” 和 “*个x,2个1,y个*”,代表的字符串分别是“(x+1)11y” 和 "x21y",即k-1个字符串为“(x+1)11y” 或 "x21y",不可能为“x1111y”.

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3776356.html

LeetCode:Count and Say的更多相关文章

  1. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  2. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  3. [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数

    You are given an integer array nums and you have to return a new counts array. The counts array has ...

  4. [LeetCode] Count Univalue Subtrees 计数相同值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  5. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  6. [LeetCode] Count Primes 质数的个数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  7. LeetCode Count of Range Sum

    原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...

  8. LeetCode Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  9. LeetCode Count Complete Tree Nodes

    原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...

  10. LeetCode——Count and Say

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

随机推荐

  1. MFC--响应鼠标和键盘操作

    一个程序最重要的部分之一是对鼠标和键盘操作的响应. 一.  理解鼠标事件.之前对鼠标事件的认识仅仅局限于处理控件的单击与双击事件.但实际鼠标的操作包含很多.这里将以一个画图的小程序讲解对鼠标的响应. ...

  2. js的闭包概念

    一.变量的作用域要懂得闭包,起首必须懂得Javascript特别的变量作用域.变量的作用域无非就是两种:全局变量和局部变量.Javascript说话的特别之处,就在于函数内部可以直接读取全局变量. J ...

  3. iOS tableView右滑显示选择

    如何使用UITableViewRowAction实现右滑选择呢? 1.在iOS8以前,我们实现tableview中滑动显示删除,置顶,更多等等的按钮时,都需要自己去实现,在iOS8中系统已经写好了,只 ...

  4. 基于WebGL的三维地形渲染

    1.生成WebMap页面 #!/usr/bin/env python # -*- coding: utf-8 -*- import subprocess from jinja2 import Envi ...

  5. Blink, 通向哈里·波特的魔法世界

    <哈里·波特>的故事里面,魔法界的新闻报纸都是动画的,配图带有动画效果.能够回放新闻的主要场景. 初次看到这个,感觉还挺新鲜的.不过现在,Blink 这样的 App 可以让这个魔法世界的幻 ...

  6. SharePoint 2013 工作流平台的选项不可用

    问题描述 当我想创建一个SharePoint 2013 工作流的时候,打开SharePoint 2013 Designer(一下简称SPD),发现没有SharePoint 2013 工作流的选项.原来 ...

  7. Android Json处理框架

    1.Android 中的Json解析工具fastjson .序列化.反序列化 2.Android Gson的使用总结 3.Android-JSONTool 一个简易的Json框架类,小到只有一个类 有 ...

  8. IOS 绘制圆饼图 简单实现的代码有注释

    今天为大家带来IOS 绘图中圆饼的实现 .h文件 #import <UIKit/UIKit.h> @interface ZXCircle : UIView @end .m文件 #impor ...

  9. 【Swift】UITableViewCell 中 TTTAttributedLabel 超链接无法点击的问题

    前言 还以为是自己代码写的有问题,用法和别的地方都一样,但是这个是在 UITableViewCell 中使用,另外在 tableHeaderView 中使用也没用这个问题 —— 使用 TTTAttri ...

  10. AndroidStudio配置gradle,让App自动签名

    最近开发关于微信一系列功能,发现分享.支付必须要打包签名才能测试,太耽误事了,耗时耗力...在网上扒拉扒拉资料,发现有很多前辈都处理过类似问题,非常感谢大家的分享,参考链接:http://blog.c ...