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.

这个题目理解起来真是太难了,尤其是英文又不好,只能参看下别人的资料,理解下规则。终于理解,题意是n=1时输出字符串1;n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,所以输出11;n=3时,由于上次字符是11,有2个1,所以输出21;n=4时,由于上次字符串是21,有1个2和1个1,所以输出1211。依次类推,写个countAndSay(n)函数返回字符串。

public String countAndSay(int n) {if (n == 1) {return "1";}// 递归调用,然后对字符串处理String str = countAndSay(n - 1) + "*";// 为了str末尾的标记,方便循环读数char[] c = str.toCharArray();int count = 1;String s = "";for (int i = 0; i < c.length - 1; i++) {if (c[i] == c[i + 1]) {count++;// 计数增加} else {s = s + count + c[i];// 上面的*标记这里方便统一处理count = 1;// 初始化}}return s;}

还有一种非递归的算法。这种我感觉比递归算法写起来就困难一些,理解起来其实差不多,双层循环。

public String countAndSay2(int n) {String oldString = "1";while (--n > 0) {StringBuilder sb = new StringBuilder();char [] oldChars = oldString.toCharArray();for (int i = 0; i < oldChars.length; i++) {int count = 1;while ((i+1) < oldChars.length && oldChars[i] == oldChars[i+1]) {count++;i++;}sb.append(String.valueOf(count) + String.valueOf(oldChars[i]));}oldString = sb.toString();}return oldString;}

[LeetCode]--38. Count and Say相关推荐

  1. LeetCode 38. Count and Say

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

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

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

  3. LeetCode: 38. Count and Say

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

  4. 51 -leetcode 38 -字符串

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

  5. 【Hard 递归 动态规划 回文串15】LeetCode 730. Count Different Palindromic Subsequences

    LeetCode 730. Count Different Palindromic Subsequences 博客转载自:http://zxi.mytechroad.com/blog/dynamic- ...

  6. leetcode python3 简单题38. Count and Say

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第三十八题 (1)题目 英文: Given an integer n where 1 ...

  7. Leetcode每日一题:38.Count and Say(外观数列)

    看题目着实难懂,评论老哥帮忙找答案,可以这么理解: 题目的意思是对序列前一个数进行报数,数列第一项为1,那第二项就报第一项的有1个1,输出11,然后第三项就在第二项的基础上报数,第二项是11,第三项不 ...

  8. LeetCode 250. Count Univalue Subtrees

    原题链接在这里:https://leetcode.com/problems/count-univalue-subtrees/ 题目: Given a binary tree, count the nu ...

  9. LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题

    题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...

最新文章

  1. 大手笔 !Julia Computing 获 2400 万美元融资,前 Snowflake CEO 加入董事会
  2. 【SRIO】5、Xilinx RapidIO核例子工程源码分析
  3. celery 运行时 (from . import async, base)SyntaxError: invalid syntax 异常的解决方案
  4. 眼图在通信系统中有什么意义_悟空CRM:施行CRM系统对汽车行业有什么意义
  5. mean,std,var
  6. spark知识点八万字长文最全汇总和高频面试题目
  7. 408. Valid Word Abbreviation有效的单词缩写
  8. android进程调试(ro.debuggable=1或android:debuggable=true)----JDWP线程
  9. 一个故事 让你看懂“区块链”
  10. 程序员技术进阶手册(一)
  11. linux登录主机命令,linux w命令查询已登录主机的用户信息
  12. Linux系统详解 第五篇:Linux的安装-4:Fedora 16的安装
  13. WPF DataGrid 导出Excel
  14. ScrollView和listview的冲突问题,关于宽度,和滑动
  15. Hinton、LeCun、Bengio等人获得2022 年阿斯图里亚斯公主技术和科学研究奖!
  16. JVM常见面试题汇总笔记
  17. 华为商城抢购插件_有赞商城社群接龙全面上线,社群营销玩法升级
  18. 高中数学40分怎么办_新高一第一次考试数学只考了40分,还有救吗?
  19. 制作一个带rEFInd引导菜单的WinPE启动光盘
  20. GaussDB架构(上)

热门文章

  1. 厦大肝癌研究成果登《细胞》顶刊背后:一作中途离世,捐献遗体用于医学研究...
  2. 魔法一样隔空在屏幕写字,捏起手指就能实现!在线可玩
  3. 哈佛博士教你如何摆脱「尬聊」
  4. 我从哆啦A梦的口袋里,掏出一辆充气电动车
  5. YOLO项目复活!大神接过衣钵,YOLO之父隐退2月后,v4版正式发布,性能大幅提升...
  6. 部署自建CA颁发证书实现https加密
  7. linux中注册系统服务—service命令的原理通俗
  8. NET(C#)连接各类数据库-集锦
  9. java动态代理(JDK和cglib)详解
  10. 电脑开机老是显示explorer.exe文件损坏