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. spring 缓存(spring自带Cache)(入门)源码解读

    spring自带的缓存类有两个基础类:Cache(org.springframework.cache.Cache)类,CacheManager(org.springframework.cache.Ca ...

  2. WCF基础知识

    根据微软官方的解释,WCF(之前的版本名为“Indigo”)是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架.它使得开发者能够建立一个跨平台的安全.可信赖.事务性 ...

  3. HDOJ 1576 A/B

    数论水题... A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. POJ 2635 The Embarrassed Cryptographer

    大数取MOD... The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1 ...

  5. 清除浮动(clearfix hack)

    eg:

  6. HTMLDOM中三种元素节点、属性节点、文本节点的测试案例

    HTML dom中常用的三种节点分别是元素节点.属性节点.文本节点. 具体指的内容可参考下图: 以下为测试用例: <!DOCTYPE html> <html> <head ...

  7. Windows下如何修改php.ini的访问路径?

    Windows下,用 IIS跑PHP,有个缺点,那就是一定要把 php.ini 放在 Windows 的系统路径下 (C:/Windows) .不过,可以通过以下方式修改访问路径: 第一步: 把PHP ...

  8. Apache 虚拟主机

    httpd支持的虚拟主机类型包括以下三种 基于域名:为每个虚拟主机使用不同的域名.但其对应的IP使相同的. 基于IP地址:为每个虚拟主机使用不同的域名,切各自对应的IP地址也不相同. 基于端口:这种方 ...

  9. DAY1 linux 50条命令

    1. tar压缩,解压缩 tar -cvf *** (压缩) tar -xvf ***  (解压缩) [root@bogon ~]# tar cvf test.tar test/ test/ test ...

  10. android service被系统回收的解决方法

    自己的app的service总是容易被系统回收,搜罗了一下,基本上的解决思路有以下几种: 1.把service写成系统服务,将永远不会被回收(未实践): 在Manifest.xml文件中设置persi ...