题目来源:https://leetcode.com/problems/count-and-say/

问题描述

38. Count and Say

Easy

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 where 1 ≤ n ≤ 30, 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"

------------------------------------------------------------

题意

给定一个由数字组成的字符串,按照数字的读法写出写一个字符串。

------------------------------------------------------------

思路

用两个指针pre和cur遍历字符串分别保存之前的字符和当前字符,再用cnt统计截止到cur,pre字符出现的次数。特别注意字符串首端和字符串尾端的处理。

------------------------------------------------------------

代码

class Solution {public String countAndSay(int n) {if (n == 0){return "";}if (n == 1){return "1";}StringBuilder sb = new StringBuilder("1");int i = 1, len = 0, j = 0, cnt = 0;char cur = 0, pre = 0;for (i=2; i<=n; i++){len = sb.length();cnt = 0;pre = 0;cur = 0;StringBuilder sb1 = new StringBuilder();for (j=0; j<len; j++){cur = sb.charAt(j);if (pre == 0){pre = cur;}else if (cur == pre){cnt++;}else{char chcnt = (char)(cnt+'1');sb1.append(chcnt);sb1.append(pre);cnt = 0;pre = cur;}}char chcnt = (char)(cnt+'1');sb1.append(chcnt);sb1.append(cur);sb = sb1;}return sb.toString();}
}

LeetCode 38. Cound and Say相关推荐

  1. LeetCode 38. Count and Say

    问题链接 LeetCode 38. Count and Say 题目解析 找规律,每一个数字串是上一个数字串的"读法".比如:n=1时为"1",读作" ...

  2. 51 -leetcode 38 -字符串

    // LeetCode 38 字符串 to_string int to string char 直接变为string //边界情况 for int i=0 i<.size() 比较的活 i+1 ...

  3. [勇者闯LeetCode] 38. Count and Say

    [勇者闯LeetCode] 38. Count and Say Description The count-and-say sequence is the sequence of integers b ...

  4. LeetCode 38外观数列39组合总和

    维护公众号:bigsai 回复bigsai分享一些学习资源! 本周上篇 LeetCode 36有效的数独&37解数独(八皇后问题) 外观数列 给定一个正整数 n(1 ≤ n ≤ 30),输出外 ...

  5. LeetCode 38. 报数

    1. 题目 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1 11 21 1211 111221 1 被读作 "one 1" ("一 ...

  6. LeetCode: 38. Count and Say

    0509第2题 题目 The count-and-say sequence is the sequence of integers with the first five terms as follo ...

  7. [LeetCode]--38. Count and Say

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

  8. leetcode @38报数-js

    题目 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1 复制代码 11 复制代码 21 复制代码 1211 复制代码 111221 复制代码 1 被读作  &qu ...

  9. leetcode 38. 外观数列 「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。前五项如下

    1.思路: 从1开始遍历,求出后面每一个转化后的数.依次求下一个. string countAndSay(int n) {string res = "1";for (int i = ...

  10. Leetcode 38.外观数列 (每日一题 20210702)

    给定一个正整数 n ,输出外观数列的第 n 项.「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述.你可以将其视作是由递归公式定义的数字字符串序列:countAndSay( ...

最新文章

  1. 调试Tomcat源码
  2. 认知推理:从图表示学习和图神经网络的最新理论看AI的未来
  3. c语言将一个已知头结点的单链表逆序_C语言数据结构实现链表逆序并输出
  4. mysql-视图、触发器、事务、存储过程、流程控制
  5. mdi 子窗体 菜单 不合并。
  6. POJ3694 Network
  7. 编译linux-2.6.29内核,报错解决方法
  8. Angular component的职责
  9. 单E1光端机分类及技术指标详解
  10. 华为鸿蒙3799跟4799有啥区别,华为鸿蒙智慧屏出世!3799元高价,是增智慧还是智商税?...
  11. 浅谈CS0433的错误的类型
  12. XAMPP中启动tomcat报错的解决方法
  13. 代码调试神器:VS Code 开源新工具!
  14. 添加jackson_Jackson,最牛掰的 Java JSON 解析器
  15. Atitit velocity 模板引擎使用法 目录 1.1. 1.4 Context 1 1.1.1. 1.4.1 Context 基本概念 1 1.2. .3不同模式下使用velocity 1
  16. 调频去加重 matlab,调频广播中预加重和去加重问题的讨论.doc
  17. 华为云notebook在线解压压缩包问题
  18. 拼多多产品怎么引流?拼多多商品怎么引更多的流量?
  19. stm32l476时钟设置
  20. 2022广东省安全员A证第三批(主要负责人)考试题库模拟考试平台操作

热门文章

  1. java面试自我介绍
  2. 从键盘上键入1~7的数字,输出对应星期以及英文缩写
  3. 去除360安全卫士的广告弹窗(亲测有效)
  4. 江苏计算机编程中小学,编程教育走进小学低年级
  5. 计算机win10搜不到wifi,Win10为何搜索不到Wifi?搜索不到Wifi的解决方法
  6. sql排序,null排在最前/最后----mysql排序之if(isnull(字段名),0,1),fild 或者 if(isnull(字段名),1,0),fild
  7. Linux的目录结构及对应目录下存放的内容
  8. 超市库存java管理系统_Java案例:超市库存管理系统
  9. DeepFace介绍
  10. Sentinel-2(哨兵2号)数据下载及预处理