LeetCode第38题

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"

翻译:

简单来说就是:

第一次:1

第二次:一个1:11

第三次:两个1:21

第四次:一个2,一个1:1211

第五次:一个1,一个2,两个1:111221

第六次:三个1,两个2,一个1:312211

以此类推...

思路:

首先想想每一次的比较,肯定是首先固定一个数值,然后循环遍历与其相比较

for(int i = 1;i<value.length();i++){
if(temp == value.charAt(i)){
count++;
}else{
//第一次出现不相等
result.append(count).append(temp);
count = 1;
temp = value.charAt(i);
}
}

else方法块里的就是关键:一旦出现不相等的情况,就append前面的结果,然后更新那个固定值,接着继续遍历比较

最外面的话很明显就是采用递归了,从1开始,一轮一轮的计算上去,直到想要的结果

String result = "1";
while(--n>0){
result = getOnce(result);
System.out.println(result);
}

完整代码如下

class Solution {
public String countAndSay(int n) {
String result = "1";
while(--n>0){
result = getOnce(result);
System.out.println(result);
}
return result;
} public String getOnce(String value){
StringBuffer result = new StringBuffer();
char temp = value.charAt(0);
int count = 1;
for(int i = 1;i<value.length();i++){
if(temp == value.charAt(i)){
count++;
}else{
//第一次出现不相等
result.append(count).append(temp);
count = 1;
temp = value.charAt(i);
}
}
return result.append(count).append(temp).toString();
}
}

每次递归打印的结果就是:

11
21
1211
111221
312211

...

欢迎关注我的微信公众号:安卓圈

【LeetCode算法-38】Count and Say的更多相关文章

  1. 【算法】LeetCode算法题-Count And Say

    这是悦乐书的第153次更新,第155篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第12题(顺位题号是38).count-and-say序列是整数序列,前五个术语如下: ...

  2. LeetCode算法题-Count Binary Substrings(Java实现)

    这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串 ...

  3. LeetCode算法题-Count Primes(Java实现)

    这是悦乐书的第190次更新,第193篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第49题(顺位题号是204).计算小于非负数n的素数的数量.例如: 输入:10 输出:4 ...

  4. LeetCode题解38.Count and Say

    38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...

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

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

  6. 【LeetCode】38 - Count and Say

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

  7. LeetCode算法题-Subdomain Visit Count(Java实现)

    这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...

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

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

  9. LeetCode - 38. Count and Say

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

随机推荐

  1. win2008r2 32位odbc安装笔记

    这ORACLE也太难用了,想简单点了事只用个ODBC CLIENT都是件麻烦事,总结了一下,安装流程如下: 1.去官网或其它地方下载: 64位: instantclient-basic-windows ...

  2. 解决PHP处理图片时内存占用过高问题

    用过GD库的同学可能都知道,使用imagecreatetruecolor()函数创建一个真彩色的画布是第一步.但是,如果画布的宽高超过平常的宽高,会带来极大的内存消耗.比如,一个9600×4800的画 ...

  3. 关闭centos大页及swappiness

    首先检查THP的启用状态: [root@localhost ~]# cat /sys/kernel/mm/transparent_hugepage/defrag [always] madvise ne ...

  4. $().on()的知识点

    事件冒泡阶段:事件从事件目标(target)开始,往上冒泡直到页面的最上一级标签. 假设一个元素div,它有一个下级元素p.<div> <p>元素</p></ ...

  5. vue解决大文件断点续传

    一.概述 所谓断点续传,其实只是指下载,也就是要从文件已经下载的地方开始继续下载.在以前版本的HTTP协议是不支持断点的,HTTP/1.1开始就支持了.一般断点下载时才用到Range和Content- ...

  6. WinDbg常用命令系列---显示局部变量dv

    dv (Display Local Variables) dv命令显示当前作用域中所有局部变量的名称和值. dv [Flags] [Pattern] 参数: Flags显示其他信息.可以包括以下任何区 ...

  7. C++对象内存布局,this指针,对象作为参数,作为返回值

    class TestClass { public: void setNum(int num) { m_num1 = num; } int getNum() { return m_num1; } pri ...

  8. linux date获取时间戳

    linux 时间戳格式 年月日 时分秒: `date ‘+%Y%m%d%H%M%S’`date +%Y%m%d%H%M%S // 年月日 时分秒date +%s // 从 1970年1月1日零点开始到 ...

  9. AD域服务器组策略实现文件夹重定向 保护文件安全

     网络管理员或许都遇到过类似的烦恼.如系统崩溃后,原来存放在 C盘的应用程序专署数据都丢失了;或者桌面上的文件莫名其妙少了,等等.有时候,我们也千方百计提醒员工不要把文件存放在桌面上,但是他们总是不听 ...

  10. 【转】Java 8新特性(四):新的时间和日期API

    Java 8另一个新增的重要特性就是引入了新的时间和日期API,它们被包含在java.time包中.借助新的时间和日期API可以以更简洁的方法处理时间和日期. 在介绍本篇文章内容之前,我们先来讨论Ja ...