38、Count and Say

The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2. 11
3. 21
4. 1211
5. 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 term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1
Output: "1"

Example 2:

Input: 4
Output: "1211"

C+

class Solution {
public:
string countAndSay(int n) {
if (n == ) return "";
string res = "";
while (--n) {
string cur = "";
for (int i = ; i < res.size(); i++) {
int count = ;
while ((i + < res.size()) && (res[i] == res[i + ])){
count++;
i++;
}
cur += to_string(count) + res[i];
}
res = cur;
}
return res;
}
};

python

Solution 1 ... using a regular expression
def countAndSay(self, n):
s = ''
for _ in range(n - 1):
s = re.sub(r'(.)\1*', lambda m: str(len(m.group(0))) + m.group(1), s)
return s
Solution 2 ... using a regular expression
def countAndSay(self, n):
s = ''
for _ in range(n - 1):
s = ''.join(str(len(group)) + digit
for group, digit in re.findall(r'((.)\2*)', s))
return s
Solution 3 ... using groupby
def countAndSay(self, n):
s = ''
for _ in range(n - 1):
s = ''.join(str(len(list(group))) + digit
for digit, group in itertools.groupby(s))
return s

leetcode 31-40 easy的更多相关文章

  1. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  2. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  3. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

  4. 剑桥offer(31~40)

    31.题目描述 统计一个数字在排序数组中出现的次数. 思路:找到最低和最高,相减 class Solution { public: int GetNumberOfK(vector<int> ...

  5. 【剑指Offer】俯视50题之31 - 40题

    面试题31连续子数组的最大和 面试题32从1到n整数中1出现的次数 面试题33把数组排成最小的数 面试题34丑数 面试题35第一个仅仅出现一次的字符 面试题36数组中的逆序对 面试题37两个链表的第一 ...

  6. [LeetCode] 31. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  7. LeetCode 31:递归、回溯、八皇后、全排列一篇文章全讲清楚

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天我们讲的是LeetCode的31题,这是一道非常经典的问题,经常会在面试当中遇到.在今天的文章当中除了关于题目的分析和解答之外,我们还会 ...

  8. Java实现 LeetCode 31下一个排列

    31. 下一个排列 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许 ...

  9. LeetCode - 31. Next Permutation

    31. Next Permutation Problem's Link ---------------------------------------------------------------- ...

  10. leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

    Next Permutation  Implement next permutation, which rearranges numbers into the lexicographically ne ...

随机推荐

  1. 图像的K-L变换

    1 问题的提出 2 K-L变换的原理 3 K-L变换的计算过程 4 K-L变换的性质 5 K-L变换的深入讨论 6 K-L变换的应用

  2. 如何给Apache Pig自定义UDF函数?

    近日由于工作所需,需要使用到Pig来分析线上的搜索日志数据,散仙本打算使用hive来分析的,但由于种种原因,没有用成,而Pig(pig0.12-cdh)散仙一直没有接触过,所以只能临阵磨枪了,花了两天 ...

  3. BZOJ 2165: 大楼

    Time Limit: 40 Sec Memory Limit: 259 MB Submit: 957 Solved: 353 [Submit][Status][Discuss] Descriptio ...

  4. js的深复制与浅复制

    什么是深复制和浅复制? 深复制和浅复制的概念只存在于对象array和数组obj上. 浅复制是:模糊复制,就是不管对方是字符串类型还是引用类型都通通复制过来.结果两个变量的内容会同时变化. 深复制是:有 ...

  5. ajax传输数据

    AJAX向后台传输数//1 直接在url中传递 $.ajax({ type: "POST", url: "/testAjax/addUser?id=1&name= ...

  6. IE6下extjs 弹窗不加载内容(无法执行内部js)的解决方案

    //需要导入的文件,这里的路径需要些你自己的路径 <link rel="stylesheet" type="text/css" href="ex ...

  7. ajax原理及使用

    1.关于同步和异步 异步传输是面向字符的传输,它的单位是字符:而同步传输是面向比特的传输,它的单位是桢,它传输的时候要求接受方和发送方的时钟是保持一致的. 具体来说,异步传输是将比特分成小组来进行传送 ...

  8. tensorflow 利用python generate_cifar10_tfrecords.py --data-dir=${PWD}/cifar-10-data报错

    报的错误如下:UnicodeDecodeError: 'ascii' codec can't decode byte 0x9e in position 0: ordinal not in range( ...

  9. fft模板 HDU 1402

    // fft模板 HDU 1402 #include <iostream> #include <cstdio> #include <cstdlib> #includ ...

  10. Redis源码解析:27集群(三)主从复制、故障转移

    一:主从复制 在集群中,为了保证集群的健壮性,通常设置一部分集群节点为主节点,另一部分集群节点为这些主节点的从节点.一般情况下,需要保证每个主节点至少有一个从节点. 集群初始化时,每个集群节点都是以独 ...