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. STM32 ~ CH340在STM32实现一键下载电路

    在做基于STM32的多功能MP3播放器的课题时,在程序下载这部分时借鉴了正点原子开发板上的一键下载电路,采用CH340G这款芯片设计. 在画PCB初期原理图部分,对采用CH340G设计的一键下载电路不 ...

  2. SOA宣言和微服务特点

    如果从概念层来看,我更喜欢把SOA归为企业架构的范畴,从企业架构出发把业务分解为不同业务域的服务,关注系统间的服务互联互通的规范,并不关心如何实现.也就是说在企业架构上使用SOA支撑业务,而在方案架构 ...

  3. 验证——正则<37>

    1,郵箱合法性驗證 /* * 郵箱合法性驗證 * @method matchTel * @papram{string} str,電子郵箱 * @return{boolean} * */ functio ...

  4. R语言的输出函数cat,sink,writeLines,write.table

    根据输出的方向分为输出到屏幕和输出到文件. 1.cat函数即能输出到屏幕,也能输出到文件. 使用方式:cat(... , file = "", sep = " " ...

  5. Android系统--灯光系统驱动编写

    Android系统开发--Android灯光系统tiny4412_led_class驱动编写 框架分析 led_classdev_4412结构体 创建led_classdev_4412结构体 分配结构 ...

  6. Go Mysql驱动

    Golang中MYSQL驱动 Mysql库https://github.com/go-sql-driver/mysql Go本身不提供具体数据库驱动,只提供驱动接口和管理. 各个数据库驱动需要第三方实 ...

  7. HDU 3466 Proud Merchants 排序 背包

    题意:物品有三个属性,价格p,解锁钱数下线q(手中余额>=q才有机会购买该商品),价值v.钱数为m,问购买到物品价值和最大. 思路:首先是个01背包问题,但购买物品受限所以应先排序.考虑相邻两个 ...

  8. [转载]spring security 的 logout 功能

    原文地址:security 的 logout 功能">spring security 的 logout 功能作者:sumnny 转载自:http://lengyun3566.iteye ...

  9. 【转载】JAVA多线程读取、操作List集合

    本文转载自:http://blog.csdn.net/wang1989cs/article/details/47663565 import java.util.ArrayList; import ja ...

  10. MyBatis之xml配置配置文件(一)(xml配置文件)

    一.properties标签 <properties resource="jdbc.properties"></properties> 使用properti ...