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. linux各个文件夹作用

    linux /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基点,比如用户user的主目录就是/ho ...

  2. PyQt4 调用串口API pySerial API说明

    pySerial API官方介绍链接 http://pyserial.readthedocs.io/en/latest/pyserial_api.html

  3. 【转】Python爬虫_示例2

    爬虫项目:爬取并筛选拉钩网职位信息自动提交简历   一 目标站点分析 #一:实验前准备: 浏览器用Chrome 用Ctrl+Shift+Delete清除浏览器缓存的Cookie 打开network准备 ...

  4. or and 运算符与 pyhton编码

    运算符 # x or y 如果 x 为真,则值为x,否则为y 1 print(4 or 3) # 4 2 print(2 or 3) # 2 3 print(1 or 3) # 1 4 print(0 ...

  5. ssh登陆virtualbox虚拟机

  6. Spring 全局异常捕捉

    Spring全局异常捕捉类 注解@ControllerAdvice package com.sicdt.sicsign.web.bill.controller; import org.springfr ...

  7. 【HackerRank】Utopian tree

    The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...

  8. 字符串哈希小结(BKDR,RK)

    前言 A:这么简单的东西,怎么现在才学?? B:别提了,还不是因为菜o(╥﹏╥)o A:那打算讲些什么东西 B:\(BKDRHash\).\(Rabin-karp\)以及简单应用 简洁 所谓字符串哈希 ...

  9. com.android.dex.DexIndexOverflowException: Cannot merge new index 66299 into a non-jumbo instruction

    打包时控制台输出: Error:Execution failed for task ':app:transformClassesWithDexForAll32Release'. > com.an ...

  10. 高通Android display分析【转】

    本文转载自:http://blog.csdn.net/zhangchiytu/article/details/6777039 高通7系列硬件架构分析 如上图,高通7系列 Display的硬件部分主要由 ...