题目:(据说是facebook的面试题哦)

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个1,记录为11,那第三个就是对11的读法就是2个1,记录为21,第四个就是对21的读法,一个2一个1,记录为1211,一次类推估计应该知道什么意思了吧。Facebook出这样的题果然思维略叼啊。

leetcode把这题label为easy,题目都这么难理解起码medium啊哈哈。

在刚才错误的基础上,在加一个循环调用就可以了。即对 给定一个string后,输出它的读法 进行循环n次就得到n的答案了。如果n为零就输出“”。

class Solution {
public:
string getNext(string s)
{
if (s == "") return "1";
string ans = "";
int len = s.size(), cnt = 1; // 计数cnt初始为1,因为一旦出现一个数那至少是一次
for (int i = 0; i < len; i++)
{
if (i+1 < len && s[i+1] == s[i])
{
cnt++;continue;
}
ans += cnt + '0'; // 和下面的s[i]要分开来加,否则会先进行字符串相加再到ans中
ans += s[i];
cnt = 1; //记录后记得将cnt恢复为1
}
return ans;
}
string countAndSay(int n)
{
if (n == 0)
return "";
string ans = "";
for (int i = 0; i < n; ++i) // 重复找n次next的就可以了
ans = getNext(ans);
return ans;
}
};

leetcode第37题--Count and Say的更多相关文章

  1. [LeetCode] 系统刷题5_Dynamic Programming

    Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题 ...

  2. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  3. 【LeetCode算法-38】Count and Say

    LeetCode第38题 The count-and-say sequence is the sequence of integers with the first five terms as fol ...

  4. LeetCode 第 3 题:无重复字符的最长子串(滑动窗口)

    LeetCode 第 3 题:无重复字符的最长子串 (滑动窗口) 方法:滑动窗口 滑动窗口模板问题:右指针先走,满足了一定条件以后,左指针向前走,直到不满足条件. 特点:左右指针的方向是一致的,并且是 ...

  5. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  6. [LeetCode每日一题]781. 森林中的兔子

    [LeetCode每日一题]781. 森林中的兔子 问题 森林中,每个兔子都有颜色.其中一些兔子(可能是全部)告诉你还有多少其他的兔子和自己有相同的颜色.我们将这些回答放在 answers 数组里. ...

  7. 【LeetCode每日一题 Day 1】1. 两数之和

    大家好,我是编程熊,今天是LeetCode每日一题的第一天,今天的你比昨天更加优秀啦! 题意 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target ...

  8. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  9. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

随机推荐

  1. iOS_21团购_发送请求【点评】数据

    结果表明,一个简单的请求: 用到的点评封装的类: 使用tableView简单展示: // // DealListController.m // 帅哥_团购 // // Created by beyon ...

  2. 设计管理员表;webservice用于网络安全的高端内提供服务的

    admin表设计.你应该有角色表,管理员属于一个样的作用,另一个接口选项,以查看表.角色有更多的选择的能力. 角色和选项代表了许多关系,因此,我们必须保持这种关系有一个表 版权声明:本文博客原创文章, ...

  3. cocos2dx怎样设置ios和Android横屏竖屏的几种方法

    cocos2d-x编译到ios上.默认是横屏的,若要改为http://竖屏.不同的ios版本号.方法也会不同 在ios7上或许我们设置好了横竖屏.但到了ios6上或许会变化.以下白白给大家分享一下我的 ...

  4. Java多线程中wait, notify and notifyAll的使用

    本文为翻译文章,原文地址:http://www.journaldev.com/1037/java-thread-wait-notify-and-notifyall-example 在Java的Obje ...

  5. hdu3480二维斜率优化DP

    Division Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others) Tota ...

  6. Android学习路线(十一)管理Activity的生命周期

    当一个用户进入.退出,再次进入你的应用时,你的应用中的Activity 会在它的生命周期的各个状态下切换. 比如,当你的activity第一次启动.它出如今系统的前方接受用户的焦点.在这个过程中,An ...

  7. Cocos2d-x 脚本语言Lua使用

    Cocos2d-x 脚本语言Lua使用 前面几篇博客已经把Lua的相关基础知识介绍了.本篇博客就来介绍一下,怎样在Cocos2d-x项目中使用Lua这门脚本语言进行开发.因为笔者使用的时Mac系统.所 ...

  8. JUnit4.8.2来源分析-2 org.junit.runner.Request

    JUnit4.8.2源代码,最为yqj2065兴趣是org.junit.runner.Request,现在是几点意味着它? ①封装JUnit的输入 JUnit4作为信息处理单元,它的输入是单元測试类- ...

  9. Claris and XOR

    Problem Description Claris loves bitwise operations very much, especially XOR, because it has many b ...

  10. 使用SeekBar办Android调色板

    1.接口布局xml代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...