leetcode 31-40 easy
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
1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211.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的更多相关文章
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- 剑桥offer(31~40)
31.题目描述 统计一个数字在排序数组中出现的次数. 思路:找到最低和最高,相减 class Solution { public: int GetNumberOfK(vector<int> ...
- 【剑指Offer】俯视50题之31 - 40题
面试题31连续子数组的最大和 面试题32从1到n整数中1出现的次数 面试题33把数组排成最小的数 面试题34丑数 面试题35第一个仅仅出现一次的字符 面试题36数组中的逆序对 面试题37两个链表的第一 ...
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode 31:递归、回溯、八皇后、全排列一篇文章全讲清楚
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天我们讲的是LeetCode的31题,这是一道非常经典的问题,经常会在面试当中遇到.在今天的文章当中除了关于题目的分析和解答之外,我们还会 ...
- Java实现 LeetCode 31下一个排列
31. 下一个排列 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许 ...
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
随机推荐
- 安装elasticsearch-head插件
安装node ###下载 wget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-x64.tar.xz ### 解压 tar xvf nod ...
- ES6 学习笔记(基础)
书链接:http://es6.ruanyifeng.com/ #.let let 不存在“变量提升” 暂时性死区(即:let 所定义的变量在局部作用域中不受外界影响) var tmp = 123; i ...
- PIL的ImageDraw的颜色问题
因为我的图片的单通道的,所以用 draw = ImageDraw.Draw(image) im_width, im_height =], info[], info[], info[]) color=d ...
- OSG实现正八面体剖分成球
#include<Windows.h> #include<osg/Node> #include<osg/Geode> #include<osg/Group&g ...
- .net 设置默认首页
解决方案一:设置默认首页 在 Web.config 文件中,加上红色字体间的内容 <configuration> <system.web> <compilation de ...
- mv- Linux必学的60个命令
1.作用 mv命令用来为文件或目录改名,或者将文件由一个目录移入另一个目录中,它的使用权限是所有用户.该命令如同DOS命令中的ren和move的组合. 2.格式 mv[options] 源文件或目录 ...
- let和const的一些知识点
let和const 不可以重复声明 不会发生变量提升,因此必须在声明之后使用,否则报错! 只在声明所在的块级作用域内有效 let 同一个作用域内不能重复声明同一个变量: function func() ...
- 最小费用最大流——EK+SPFA
终于把最小费用最大流学会了啊-- 各种奇奇怪怪的解释我已经看多了,但在某些大佬的指点下,我终于会了. 原来是个好水的东西. 最小费用最大流是什么? 不可能不知道网络流吧?如果不知道,自行百度去-- 费 ...
- Linux跨PC拷贝之SCP
命令:scp 不同的Linux之间copy文件常用有3种方法: 第一种就是ftp,也就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的client程序来进行文件的copy. ...
- Django项目:CRM(客户关系管理系统)--73--63PerfectCRM实现CRM讲师下载作业
# teacher_urls.py # ————————62PerfectCRM实现CRM讲师讲课记录———————— from django.conf.urls import url from bp ...