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的更多相关文章

  1. 【一天一道LeetCode】#38. Count and Say

    一天一道LeetCode系列 (一)题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, ...

  2. 【LeetCode】38 - Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  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 - 38. Count and Say

    38. Count and Say Problem's Link ------------------------------------------------------------------- ...

  5. [LeetCode] 38. Count and Say 计数和读法

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

  6. 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 ...

  7. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

  8. 【LeetCode题解】347_前K个高频元素(Top-K-Frequent-Elements)

    目录 描述 解法一:排序算法(不满足时间复杂度要求) Java 实现 Python 实现 复杂度分析 解法二:最小堆 思路 Java 实现 Python 实现 复杂度分析 解法三:桶排序(bucket ...

  9. 【LeetCode题解】2_两数相加

    目录 [LeetCode题解]2_两数相加 描述 方法一:小学数学 思路 Java 代码(非递归写法) Java 代码(递归写法) Python 代码(非递归写法) [LeetCode题解]2_两数相 ...

随机推荐

  1. ogma

    Ogma是Linkurious的JavaScript图形可视化库.Ogma的一个实例是一个javascript对象,它在内部存储一个图形, 并根据样式规则在HTML容器中呈现它. Ogma有两个版本: ...

  2. 【java】java基本用法记录

    java用法总结 计时 long startTime = System.nanoTime(); solution.process(inputFile); long endTime = System.n ...

  3. mysql 与 oracle 的连表update

    mysql: update 表A a,表B b set a.xx=b.xx where a.id=b.id; oracle update 表A set a.xx=(select b.xx from 表 ...

  4. php配置文件参数设置

    pm.max_children 设置多大合适? 一.pm.max_children 多大合适? 这个值原则上是越大越好,php-cgi的进程多了就会处理的很快,排队的请求就会很少. 设置”max_ch ...

  5. java springboot 大文件分片上传处理

    参考自:https://blog.csdn.net/u014150463/article/details/74044467 这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时 ...

  6. JAVA取数两个数组交集,考虑重复和不重复元素

    1.考虑不重复元素,重复元素不添加 import java.awt.List; import java.util.ArrayList; import java.util.TreeSet; public ...

  7. sql 语句中count()有条件的时候为什么要加上or null

    参考:https://blog.csdn.net/qq_32719287/article/details/79513164 1.sql 语句中count()有条件的时候为什么要加上or null. 如 ...

  8. Idea远程调试undertow

    1.修改jfinal.sh 添加远程调试配置,端口5555可自行设置 JAVA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,suspend=n,s ...

  9. Docker 踩坑记(failed to build: Get https://registry-1.docker.io/v2/microsoft/dotnet/manifests/2.1-sdk: unauthorized: incorrect username or password)

    今天看了下.net core 示例项目eShopWebOnline. 无奈在使用docker的时候总是提示一下错误信息,大致信息是用户名密码错误.但是,明明桌面右下角Docker帐号处于登录状态. E ...

  10. 咸鱼入门到放弃12--Filter(过滤器)*

    一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...