String

Description:

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, 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"

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

在此说明一下题意:

例如,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();
}
}

LeetCode & Q38-Count and Say-Easy的更多相关文章

  1. [LeetCode] 038. Count and Say (Easy) (C++/Python)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...

  2. 【leetcode】Count Primes(easy)

    Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...

  3. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  4. 【leetcode】Count and Say (easy)

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  5. (easy)LeetCode 204.Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...

  6. Leetcode解题思路总结(Easy篇)

    终于刷完了leetcode的前250道题的easy篇.好吧,其实也就60多道题,但是其中的套路还是值得被记录的. 至于全部code,请移步github,题目大部分采用python3,小部分使用C,如有 ...

  7. leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  8. 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum

    又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...

  9. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  10. leetcode@ [327] Count of Range Sum (Binary Search)

    https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...

随机推荐

  1. Python模块之hashlib模块、logging模块

    一.hashlib模块 hashlib模块介绍:hashlib这个模块提供了摘要算法,例如 MD5.hsa1 摘要算法又称为哈希算法,它是通过一个函数,把任意长度的数据转换为一个长度固定的数据串,这个 ...

  2. java--抽象类实例(包含静态内部抽象类)

    静态内部抽象类可以被继承. public class testfather { public static void main(String[] args) { person.talk2 a = ne ...

  3. intellij idea快捷键字典

    最近在重装系统,在安装python IDE时候依然安装了sublime Text3和intellij Idea(冏,别问为什么没安装pycharm,0-0 逃).首先是已然将之前一直使用的sublim ...

  4. 牛客小白月赛1 A-简单题

    描述 Etéreo 是个爱学习的好孩子.在年假期间,他依然热情于数学.他最近发现了一个高大上的东西:,他觉得这里的  非常的厉害!然后他又告诉你:,,他会告诉你  和  ,想请你告诉他  的值.当然这 ...

  5. MarkDown 的使用学习

    以下都是MarkDown常用的语法以及效果图: ### 生成目录 [TOC] ### 标题 标题的标签采用#加文本的形式,几个星就代表几级标题 在#后加空格 # 一级标题 ## 二级标题  ... # ...

  6. C# 中的关键字整理

    C#中的所有关键字大致可以分为保留关键字和上下文关键字两部分 快来看一下有没有不认识的吧 欢迎补充 保留关键字 abstract  as  base  bool  break byte  case  ...

  7. selenium-01 搭建环境

    很老套,但是不得不做的事. selenium的发展史,具体的虫师大大已经写得很具体了:http://www.cnblogs.com/fnng/p/3653793.html 1.第一种使用的姿势就是ID ...

  8. Android工程化开发这门学科的看法

    http://www.cnblogs.com/unruledboy/p/DevCareer.html http://coolshell.cn/articles/4561.html http://blo ...

  9. 插入排序(Java实现)

    直接插入排序 public class InsertionSort { public static <T extends Comparable<? super T>> void ...

  10. Django+xadmin打造在线教育平台(三)

    五.完成注册.找回密码和激活验证码功能 5.1.用户注册 register.html拷贝到templates目录 (1)users/views.py class RegisterView(View): ...