【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 ------------------------------------------------------------------- ...
随机推荐
- 计算机网络基础之IP地址详解
计算机网络基础之IP地址详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.IP地址概述 1>.什么是IP地址 我们为什么要使用逻辑地址(IP地址)来标识网络设备,而不采 ...
- 如何更改scratch3.0的文字
首先,我们来看以下的图,我们需要更改scratch3.0的文字,例如文件,新作品,从电脑上传等文字. 打开源码,目录src/compents/menu-bar/menu-bar.jsx 大家会发现,所 ...
- springmvc手动获取bean
@Service @Lazy(false) public class SpringContextHolder implements ApplicationContextAware, Disposabl ...
- SQL进阶系列之5外连接的用法
写在前面 SQL本身是作为一种数据提取工具而出现,使用SQL生成各种定制化报表和非定制化报表并非SQL原本用途的功能,但这并不意味着SQL无法实现这些功能. 用外连接进行行列转换(1)(行 → 列): ...
- 【Beta】Scrum meeting3
第三天:2019/6/26 前言: 第3次会议于6月26日在教9-501召开. 对每个人负责撰写的文档进行分配,并讨论其中模糊的问题,时长30min. 本日任务完成情况 成员 今日完成任务情况 成员贡 ...
- app安全测试初级
分析方法:静态分析 主要是利用apktool.dex2jar.jd-gui.smali2dex等静态分析工具对应用进行反编译,并对反编译后的java文件.xml文件等文件进行静态扫描分析,通过关键词搜 ...
- K3二次开发后台表
select * from icclasstype where fname_chs like '%供货%' 用此表基本上可以查询到所有的表 select * from POrequest --采购申请 ...
- 四行公式推完神经网络BP
据说多推推公式可以防止老年痴呆,(●ˇ∀ˇ●) 偶尔翻到我N年前第一次推导神经网络的博客居然四页纸,感慨毅力! http://blog.sina.com.cn/s/blog_1442877660102 ...
- L1434滑雪
一,看题 1,这个长度怎么算的. 从它自己数,可以走下去的位置. 2,这个题的衣服怎么披上去呀. 3,搜索目标,状态. 肯定要用坐标,不然怎么搜索. 4,在前期还是多写把. 5,我靠这个点还是随机的& ...
- inflection point
http://blog.thefirehoseproject.com/posts/learn-to-code-and-be-self-reliant/ kill will develop 1.repe ...