【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 ------------------------------------------------------------------- ...
随机推荐
- Python入门篇-返回值和作用域
Python入门篇-返回值和作用域 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.返回值 1>.返回值概述 Python函数使用return语句返回“返回值” 所有函数都 ...
- Linux跑脚本用sh和./有什么区别?
一个很有意思的例子: sh是一个shell.运行sh a.sh,表示我使用sh来解释这个脚本:如果我直接运行./a.sh,首先你会查找脚本第一行是否指定了解释器,如果没指定,那么就用当前系统默认的sh ...
- Zabbix导入MySQL数据库报错ERROR 1046 (3D000) at line 1: No database selected
使用如下命令导入Zabbix数据库时报错 解决办法: 1.先把原始的数据库压缩包备份 cd /usr/share/doc/zabbix-server-mysql-4.0.7/ cp create.sq ...
- HDU - 3311: Dig The Wells (斯坦纳树)
题意:给你n个寺庙,m个村庄,p条路,现在你要在这n+m个位置中选出若干个位置打井,每个位置打井的费用会告诉你,同时p条路也有修建费用,现在每个寺庙都住着一个和尚,问你最小的费用让这n个和尚都能喝上水 ...
- 异常检测(Anomaly detection): 高斯分布(正态分布)
高斯分布 高斯分布也称为正态分布,μ为平均值,它描述了正态分布概率曲线的中心点.σ为标准差,σ2为方差,σ描述了曲线的宽度.在中心点附近概率密度大,远离中心点概率密度小. 高斯分布图 概率曲线下方的面 ...
- LeetCode 741. Cherry Pickup
原题链接在这里:https://leetcode.com/problems/cherry-pickup/ 题目: In a N x N grid representing a field of che ...
- hibernate实现增删改查
1.需要先创建学生实体: package pers.zhb.domain; public class Student { private int studentno; private String s ...
- zeebe 0.20.0 发布生产可用了!
一个比较好消息,来自camunda zeebe 团队的消息,zeebe 0.20.0 发布,终于可以生产可用了 如果关注了官方的声明的话,同时团队也出了一个自己的许可协议,但是和大部分当前的开源 产品 ...
- 开源项目 02 HttpLib
using JumpKick.HttpLib; using Newtonsoft.Json; using System; using System.Collections.Generic; using ...
- 查全率(Recall),查准率(Precision),灵敏性(Sensitivity),特异性(Specificity),F1,PR曲线,ROC,AUC的应用场景
之前介绍了这么多分类模型的性能评价指标(<分类模型的性能评价指标(Classification Model Performance Evaluation Metric)>),那么到底应该选 ...