LeetCode题解38.Count and Say
38. Count and Say
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.
My Thought
理解题目理解了半天。。。
好吧,大致意思是,第一个数字是“1”(字符串的形式)。从第二个数字开始,字符串按照前一个字符串的读法决定。比如,前一个是“1”,那么就是1个“1”,于是第二个字符串是“11”,依次类推。
一个递归解决问题。
伪代码:
PROCEDURE countAndSay(n)
if n = 1
return "1"
else
// 获取前一个字符串
preString = countAndSay(n-1)
// 按规则读字符串,作为返回串、
return read(preString)
Code(C++ 0ms)
class Solution {
public:
string countAndSay(int n) {
if(n==1)
return "1";
string pre = countAndSay(n-1);
// read rule
pre+='#';
string ret="",temp="";
int num = 1;
char ch=pre[0];
for(int i=1;i<pre.size();++i){
if(pre[i]!=ch){
temp+=('0'+num);
temp+=ch;
ret.append(temp);
temp="";
ch=pre[i];
num=0;
}
num+=1;
}
return ret;
}
};
最后0ms ACC 镇啊,激动(虽然很ez23333
题解在我的github上会第一时间更新,有兴趣的可以star一下
LeetCode题解38.Count and Say的更多相关文章
- 【一天一道LeetCode】#38. Count and Say
一天一道LeetCode系列 (一)题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, ...
- 【LeetCode】38 - Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 【LeetCode算法-38】Count and Say
LeetCode第38题 The count-and-say sequence is the sequence of integers with the first five terms as fol ...
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- leetCode练题——38. Count and Say
1.题目 38. Count and Say The count-and-say sequence is the sequence of integers with the first five te ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- 【LeetCode题解】347_前K个高频元素(Top-K-Frequent-Elements)
目录 描述 解法一:排序算法(不满足时间复杂度要求) Java 实现 Python 实现 复杂度分析 解法二:最小堆 思路 Java 实现 Python 实现 复杂度分析 解法三:桶排序(bucket ...
- 【LeetCode题解】2_两数相加
目录 [LeetCode题解]2_两数相加 描述 方法一:小学数学 思路 Java 代码(非递归写法) Java 代码(递归写法) Python 代码(非递归写法) [LeetCode题解]2_两数相 ...
随机推荐
- Linux 常用命令介绍
介绍常用命令,在忘记时便于即使查询 复制.移动.删除 cp.mv.rm.pwd 1. CP 介绍 用法:CP [-adfilprsu] 源文件 目标文件 参数:参数说明: -a:是指arc ...
- django实现SSO
前言 公司的各种运维平台越来越多,用户再每个平台都注册账号,密码,密码太多记不住不说,然后有的平台过一段时间还得修改密码,烦!还不如弄个统一登录平台!! 需求分析 造这辆大车,首先就得造两个轮子 首先 ...
- docker删除已经停止的容器
前言:docker容器已经停止运行的容器,怎么清理 1.如图: docker ps -a :显示所有运行过的docker容器 status : docker容器的状态 docker ...
- C# 操作docx文档
一.需要引用DocX类库文件,可以直接在NuGet中找到. 二.创建文件,并添加一张表格 public static string fileName = AppDomain.CurrentDomain ...
- CTO 能力模型(简化版)
最近思考了很多,我在大贲这几年的工作内容.从一开始到现在,伴随着大贲从一二十人,走到了现在的两百多人.我的工作也从一开始的带头冲锋陷阵,逐步转移到了带领产品研发,再到后来的全公司多业务线的技术管理工作 ...
- Linux命令学习总结之rmdir命令的相关资料可以参考下
这篇文章主要介绍了Linux命令学习总结之rmdir命令的相关资料,需要的朋友可以参考下(http://www.nanke0834.com) 命令简介: rmdir命令用用来删除空目录,如果目录非空, ...
- Macbook pro从购买服务器到搭建服务器环境(1)
查看进程命令:ps -ef |grep *** ps -ef | grep nginx ps -ef | grep mysql ps -ef | grep mongops -ef | grep uws ...
- linux查看空间情况----df与du命令
1.查看整个磁盘空间使用情况,可以使用命令 :df -h 2.获取当前的文件夹下的磁盘使用情况,可以使用如下命令:du --max-depth=1 -h 使用 du --max-depth=1 - ...
- 权限的控制 shiro的使用
******shiro的使用 三大核心组件 Subject:即当前用户,在权限管理的应用程序里往往需要知道谁能够操作什么,谁拥有操作该程序的权利,shiro中则需要通过Subject来提供基础的当前用 ...
- 2019-3-22c# TextBox只允许输入数字,禁用右键粘贴,允许Ctrl+v粘贴数字
TextBox 禁止复制粘贴 ShortcutsEnabled =false TextBox只允许输入数字,最大长度为10 //TextBox.ShortcutsEnabled为false 禁止右键和 ...