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.

十天没做题了,赶快秒个小题练练手。

思路:就是数每种数字出现了几次。

string countAndSay(int n) {
string s = "";
while(--n) //从1开始计数
{
string stmp;
char c[];
for(int i = ; i < s.length(); )
{
int count = ;
char cur = s[i];
while(i < s.length() && s[i] == cur) //数当前重复出现的数字
{
count++;
i++;
}
sprintf(c, "%d%d", count, cur - '');
stmp += c;
}
s = stmp;
}
return s;
}

【leetcode】Count and Say (easy)的更多相关文章

  1. 【leetcode】Reverse Linked List(easy)

    Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...

  2. 【leetcode】Factorial Trailing Zeroes(easy)

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  3. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  4. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  5. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  6. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  7. 【leetcode】Word Ladder II(hard)★ 图 回头看

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  8. 【LeetCode】Agorithms 题集(一)

    Single Number 题目 Given an array of integers, every element appears twice except for one. Find that s ...

  9. 【LeetCode】Algorithms 题集(三)

    Search Insert Position 意: Given a sorted array and a target value, return the index if the target is ...

随机推荐

  1. [译]Mongoose指南 - Plugin

    Schema支持插件, 这样你就可以扩展一些额功能了 下面的例子是当document save的时候自定更新最后修改日期的出插件 // lastMod.js module.exports = expo ...

  2. [译]Profile and debug your ASP.NET MVC app with Glimpse

    原文:http://www.asp.net/mvc/overview/performance/profile-and-debug-your-aspnet-mvc-app-with-glimpse Gl ...

  3. 如何确定C#代码是在编译时执行还是在运行时执行

    突然想起那个"switch..case..."的case标签都可以判断哪些类型... 就先搞了一个错误的demo... class Program { static void Ma ...

  4. iframe 内联框架

    Iframe - 设置高度和宽度 height 和 width 属性用于规定 iframe 的高度和宽度. 属性值的默认单位是像素,但也可以用百分比来设定(比如 "80%"). 实 ...

  5. PHP预编译处理技术简介

    1.提高数据库的效率:减少编译次数,减少连接次数.当出现当量操作sql语句,比如大量将数据插入数据库中,原来的那种单个执行sql语句或者批量执行sql语句的做法,显然是不可行的,因为无论是单个执行还是 ...

  6. Linux 下复制(cp)目录时排除一个或者多个目录的方法

    cp 貌似没有排除目录的功能,可以使用 rsync 命令来实现了,如: [案例] /home/52php目录里面有data目录,data目录里面有 a.b.c.d.e 五个目录,现在要把data目录里 ...

  7. netbeans tomcat

    http://www.cnblogs.com/fengzy/archive/2013/05/18/3086371.html http://blog.csdn.net/xumengxing/articl ...

  8. C 文件读写 容易疏忽的一个问题

    今天需要解决一个问题,将影像瓦片(一堆jpg文件)分别进行读取,并将所有数据以文件流的方式存入一个.db的文件中, 同时将每个jpg数据在db文件中的位置保存下来,作为index存在.idx文件中. ...

  9. test dword ptr [eax],eax ; probe page. visual studio

    当在函数中申请占用空间很大的数组.结构体时,会产生该问题. 由于局部变量的申请空间存放于栈中,windows里默认栈内存是1M 所以当申请空间大于1M时就会出现溢出错误 通过debug就会进入以下文件 ...

  10. php实现图片缩放功能类

    http://www.poluoluo.com/jzxy/201312/255447.html <?php /** * Images类是一个图片处理类 * @package applicatio ...