【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 ------------------------------------------------------------------- ...
随机推荐
- JSX 到 JS 的转换
在写react代码的时候,大部分同学应该都是写JSX.因为相比于写纯JavaScript.写JSX为我们去写组件,比写一些在JavaScript当中写类似于html结构的这种代码是要方便非常非常多的, ...
- jq function return value
所有 JS 函数 都会返回值 假如 没有 return 则返回 undefined
- POJ - 3728:The merchant (Tarjan 带权并查集)
题意:给定一个N个节点的树,1<=N<=50000 每个节点都有一个权值,代表商品在这个节点的价格.商人从某个节点a移动到节点b,且只能购买并出售一次商品,问最多可以产生多大的利润. 思路 ...
- Keil MDK5生成 .bin文件的简单教程(图文)
以下参考https://blog.csdn.net/u014563989/article/details/51127519,同时自己实测. 1.按如图步骤做,主要是要找到fromelf.exe的路径: ...
- UVa10828 Back to Kernighan-Ritchie——概率转移&&高斯消元法
题意 给出一个程序控制流图,从每个结点出发到每个后继接结点的概率均相等.当执行完一个没有后继的结点后,整个程序终止.程序总是从编号为1的结点开始.你的任务是对于若干个查询结点,求出每个结点的期望执行次 ...
- WinDbg扩展
WinDbg的扩展,也可以叫插件.它用于实现针对特定调试目标的调试功能,用于扩展某一方面的调试功能.扩展的实现是通过扩展模块(DLL)实现的.Windbg本身已经包含了很多扩展命令,这些扩展为这Win ...
- js傻瓜式制作电子时钟
js傻瓜式制作电子时间 使用到的知识点 setInterval函数 构建函数new Date if判断 demo: //css样式请自行设置 <span id="timer" ...
- THUPC&CTS 2019 游记
day ? 去THU报了个到. day? THUPC比赛日,三个人都没有智商,各种签到题不会做,被各路神仙吊着打.G题还猜了个假结论,做了好久都不对.最后顺利打铁了. 还顺便去看一下THUAC. da ...
- 深入浅出的Java网络通信
已经发表个人公众号 代码展示 package two; import java.io.BufferedReader; import java.io.InputStreamReader; import ...
- 第12组 Alpha冲刺(4/6)
Header 队名:To Be Done 组长博客 作业博客 团队项目进行情况 燃尽图(组内共享) 由于这两天在修bug,燃尽图没有下降 展示Git当日代码/文档签入记录(组内共享) 注: 由于Git ...