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.

好奇怪的一道题目,意思是数(三)数(四).把数字用口头语言说出来,1就是1,2前面是1就是1一个(11),3前面2个1就是(21),然后是(1211),再是(111221)以此类推。。。 当时题目看了半天没看懂,又去别的地方查了下题目是什么意思才知道了:

 class Solution {
public:
string countAndSay(int n) {
string curr = "";
if(n <= ) return curr;
curr = "";
for(int i = ; i < n; ++i){
curr = convert(curr);
}
return curr;
} string convert(const string & prev){
//string result = "";
stringstream result;
char last = prev[];
int count = ;
int sz = prev.size();
for(int i = ; i <= sz; ++i){//注意是<=
if(prev[i] == last)
count++;
else{
result << count << last;
// result.append(count); 这里append无法实现,因为找不到itoa,很蛋疼,只能用stream来实现
// result.append(last);
last = prev[i];
count = ;
}
}
return result.str();
}
};

下面是java版本的,用了双指针,方法和上面的还是有一点不一样的:

 public class Solution {
public String countAndSay(int n) {
String s = String.valueOf(1);//很方便,直接就有类似itoa的api
for(int i = 1; i < n; ++i){
s = say(s);
}
return s;
} public String say(String s){
int sz = s.length();
int p2 = 0, p1 = 0;
int count = 0;
String ret = new String("");
while(p1 < sz){
while(s.charAt(p1) == s.charAt(p2)){
p1++;
if(p1 == sz) //检查如果超过了长度就退出
break;
}
count = p1 - p2;
ret = ret + String.valueOf(count) + s.charAt(p2);
p2 = p1;//更新p2
}
return ret;
}
}

LeetCode OJ:Count and Say(数数)的更多相关文章

  1. [LeetCode] 248. Strobogrammatic Number III 对称数III

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  2. [Leetcode 216]求给定和的数集合 Combination Sum III

    [题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...

  3. LeetCode(1): 两数之和

    本内容为LeetCode第一道题目:两数之和 # -*- coding: utf-8 -*- """ Created on Sun Mar 10 19:57:18 201 ...

  4. LeetCode(2): 两数相加

    本内容为LeetCode第二道题目:两数相加 # -*- coding: utf-8 -*- """ Created on Sun Mar 10 10:47:12 201 ...

  5. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

  6. 038 Count and Say 数数并说

    数数并说序列是一个整数序列,第二项起每一项的值为对前一项的计数,其前五项如下:1.     12.     113.     214.     12115.     1112211 被读作 " ...

  7. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  8. Leetcode(9)回文数

    Leetcode(9)回文数 [题目表述]: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 第一次:直接全部转 执行用时:148 ms: 内存消耗:13.4 ...

  9. 【转载】C#通过Rows.Count属性获取总行数

    在C#中的Datatable数据变量的操作过程中,有时候我们需要知道DataTable中是否含有数据行或者DataTable的数据总行数,此时我们就可以先拿到DataTable中的Rows属性对象,通 ...

  10. [LeetCode] 137. Single Number II 单独数 II

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

随机推荐

  1. 图像分割之mean shift

    阅读目的:理解quick shift,同时理解mean shift原理,mean shift用于图像聚类,优点是不需要指定聚类中心个数,缺点是计算量太大(原因). mean shift主要用来寻找符合 ...

  2. PAT 天梯赛 L2-021. 点赞狂魔 【水】

    题目链接 https://www.patest.cn/contests/gplt/L2-021 题意 给出一个若干个人名,后面给出点赞的总数,以及每个赞的标签类型,输出前三个点赞狂魔,按标签类型不同数 ...

  3. Linux服务器iops性能测试-iozone

    1. 选用工具: iozone           下载地址:http://www.iozone.org/ (直接下载rpm包) 2. 工具安装: 执行命令: rpm -ivh iozone-3-40 ...

  4. iOS KVC 和 KVO 的学习

    KVC  (NSKey Value Coding) :键值编码 KVO (Key Value Observing) :键值监听 前言:我曾经用过 监听 一个音频何时结束 监听视频播放 状态等 用了这种 ...

  5. 试坑不完美的 clip-path (我说的 CSS 的那个)

    需求跟我说,咱们要创新,想做一个蜂巢状的列表,年少无知的我竟然一口答应了,全然因为刚接触了 clip-path: But,然而,不幸的是,这只是坎坷路途的开始.... clip-path 的教程很多了 ...

  6. Cocos2d-x项目移植到WP8系列之八:CCLabelTTF显示中文不换行

    原文链接: http://www.cnblogs.com/zouzf/p/3985330.html 在wp8平台上,CCLabeTTF显示中文不会自动换行,看了下源码,原来底层的实现是根据text的空 ...

  7. 关于IIntelliJ IDEA(2017)安装和破解

    一.下载并安装, IntelliJ IDEA的官网:https://www.jetbrains.com 二.破解. 百度下载一个 JetbrainsCrack-2.6.2.jar 破解补丁.放在你的安 ...

  8. FreeMarker缓存处理

    FreeMarker 的缓存处理主要用于模版文件的缓存,一般来讲,模版文件改动不会很频繁,在一个流量非常大的网站中,如果频繁的读取模版文件对系统的负担还是很重的,因此 FreeMarker 通过将模版 ...

  9. python基础语法学习常见小问题

    说明:我是最近觉得python在完成很多工作中方便使用而且功能强大,想突击学习一下.用的是廖雪峰老师的教程,学习python3.X.这里是廖雪峰老师的网站链接: http://www.liaoxuef ...

  10. Elasticsearch6.0简介入门介绍

    Elasticsearch简单介绍 Elasticsearch (ES)是一个基于Lucene构建的开源.分布式.RESTful 接口全文搜索引擎.Elasticsearch 还是一个分布式文档数据库 ...