【LeetCode算法-38】Count and Say
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的更多相关文章
- 【算法】LeetCode算法题-Count And Say
		这是悦乐书的第153次更新,第155篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第12题(顺位题号是38).count-and-say序列是整数序列,前五个术语如下: ... 
- LeetCode算法题-Count Binary Substrings(Java实现)
		这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串 ... 
- LeetCode算法题-Count Primes(Java实现)
		这是悦乐书的第190次更新,第193篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第49题(顺位题号是204).计算小于非负数n的素数的数量.例如: 输入:10 输出: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 ... 
- 【一天一道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算法题-Subdomain Visit Count(Java实现)
		这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ... 
- [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
		38. Count and Say Problem's Link ------------------------------------------------------------------- ... 
随机推荐
- Zabbix 完整的监控流程
			目录 1.Zabbix的监控历程概念 1.1 基本概念 1.2 流程图 2.创建主机并加入主机组 3.添加新加主机的应用集(aplication) 4.添加监控项(item) 5.告警触发器配置(Tr ... 
- spring cloud (八) Config client 和项目公共配置
			1 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ... 
- Codeforces B. Too Easy Problems
			题目描述: time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ... 
- 使用python的jira库操作jira的版本单和问题单链接
			操作JIRA的API来实现的. 但感觉比单纯操作API要简单一些. from jira import JIRA from django.conf import settings JIRA_URL = ... 
- C#中的WinForm的消息机制简述,及消息机制下Invoke,和BeginInvoke的使用和区别
			在Invoke或者BeginInvoke的使用中无一例外地使用了委托Delegate,至于委托的本质请参考我的另一随笔:对.net事件的看法. 一.为什么Control类提供了Invoke和Begin ... 
- postgres —— 分组集与部分聚集
			创建表 create table t_oil ( region text, country text, year text, production int, comsumption int ) 导入数 ... 
- RobotFrameWork框架介绍与安装
			一.RobotFrameWork介绍 1.简称RF,基于python研发的一款自动化测试框架.开源.跨平台的测试框架 2.RF是基于RF基金来研发的一款软件,目前已完全能够在python3环境下应用 ... 
- mysql distinct()函数 去重
			mysql> select * from table1; +----------+------------+-----+---------------------+ | name_new | t ... 
- Eclipse安装jbpm插件
			1.1 eclipse mar 和neon有什么区别? Eclipse 是一个开放源代码的.基于Java的可扩展开发平台.就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境. . ... 
- sudo rm /var/cache/apt/archives/lock sudo rm /var/lib/dpkg/lock
			原因:历史软件(包)更新(安装)未完成就退出了系统 解决办法:杀死该进程 sudo rm /var/cache/apt/archives/lock sudo rm /var/lib/dpkg/lock ... 
