博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode & Q38-Count and Say-Easy
阅读量:4488 次
发布时间:2019-06-08

本文共 1225 字,大约阅读时间需要 4 分钟。

String

Description:

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

1.     12.     113.     214.     12115.     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, 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: 1Output: "1"

Example 2:

Input: 4Output: "1211"

个人觉得这个题意实在是反人类!!!

在此说明一下题意:

例如,5对应111221,6的结果就是对应读5得到的,也就是3个1、2个2、1个1,即:312211

my Solution:

public class Solution {    public String countAndSay(int n) {        StringBuffer sb = new StringBuffer("1");        for (int i = 1; i < n; i++) {            StringBuffer next = new StringBuffer();            int count = 1;            for (int j = 0; j < sb.length(); j++) {                if (j+1 < sb.length() && sb.charAt(j) == sb.charAt(j+1)) {                    count++;                } else {                    next.append(count).append(sb.charAt(j));                    count = 1;                }            }            sb = next;        }        return sb.toString();    }}

转载于:https://www.cnblogs.com/duyue6002/p/7262919.html

你可能感兴趣的文章
插入排序算法--Java实现
查看>>
android软键盘控制
查看>>
自定义LinkedList实现
查看>>
HDU 5306 线段树
查看>>
php输出json 对象{‘code’:200,'data':对象模式}
查看>>
springBean的生命周期
查看>>
【eclipse】启动不了报错java was started but returned exit code=13
查看>>
本地yum源 、阿里yum源、163yum源的配置安装
查看>>
codeforce 604B More Cowbell
查看>>
uvalive 3938 "Ray, Pass me the dishes!" 线段树 区间合并
查看>>
html中事件调用JavaScript函数时有return与没有return的区别
查看>>
[转帖]ASP.NET4中不要相信Request.Browser.Cookies,Form验证要用UseCookies
查看>>
Windows7中安装内存与可用内存不一致的解决办法
查看>>
HDU3065 AC自动机
查看>>
BUAA_OO_第一次作业总结
查看>>
数据结构-第10周作业(二叉树的创建和遍历算法)
查看>>
Java日志框架(二)
查看>>
[转载]SQL Server 2008 R2安装时选择的是windows身份验证,未选择混合身份验证的解决办法...
查看>>
[转]橘子版V880问题汇总及解决办法
查看>>
JS内置对象练习(慕课网题目)
查看>>