The count-and-say sequence is the sequence of integers beginning as follows:

1, 11, 21, 1211, 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 sequence.

Note: The sequence of integers will be represented as a string.

public class Solution {
public String countAndSay(int n) {
if (n == 0)
return "";
StringBuffer sb = new StringBuffer("1");
StringBuffer tempSB = new StringBuffer(); for (int i = 1; i < n; i++) {
int counter = 1;
char tempChar = sb.charAt(0);
for (int j = 1; j < sb.length(); j++) {
if (sb.charAt(j) == tempChar)
counter++;
else {
tempSB.append(""+counter + tempChar);
tempChar = sb.charAt(j);
counter = 1;
}
}
tempSB.append(""+counter + tempChar);
sb.delete(0, sb.length());
sb.append(tempSB);
tempSB.delete(0, tempSB.length());
}
return sb.toString();
}
}

或者

	public String countAndSay(int n) {
if (n == 0) return "";
StringBuffer sb = new StringBuffer("1");
StringBuffer tempSB = new StringBuffer(); for (int i = 1; i < n; i++) {
int counter = 1;
char tempChar = sb.charAt(0);
for (int j = 1; j < sb.length(); j++) {
if (sb.charAt(j) == tempChar)
counter++;
else {
tempSB.append(Integer.toString(counter)+ tempChar);
tempChar = sb.charAt(j);
counter = 1;
}
}
tempSB.append(Integer.toString(counter)+ tempChar);
sb.delete(0, sb.length());
sb.append(tempSB);
tempSB.delete(0, tempSB.length());
}
return sb.toString();
}

LeetCode 37 Count and Say的更多相关文章

  1. leetcode 36 有效的数独 哈希表 unordered_set unordersd_map 保存状态 leetcode 37 解数独

    leetcode 36 感觉就是遍历. 保存好状态,就是各行各列还有各分区divide的情况 用数组做. 空间小时间大 class Solution { public: bool isValidSud ...

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

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

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

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

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

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

  5. 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 ...

  6. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

  7. leetcode 315. Count of Smaller Numbers After Self 两种思路

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

  8. leetcode 730 Count Different Palindromic Subsequences

    题目链接: https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 730.Count ...

  9. LeetCode 38 Count and Say(字符串规律输出)

    题目链接:https://leetcode.com/problems/count-and-say/?tab=Description   1—>11—>21—>1211—>111 ...

随机推荐

  1. KVM资源划分分配技巧

    kvm有个叫做超分的概念,根据这个特性可以分配出超出物理机配置的数台虚拟机. 以下是自己总结的一些划分技巧: 一.最保守方法(性能最好) 根据物理机的资源,按虚拟机的数量叠加但不超过物理机的总和.不超 ...

  2. Towelroot v3.0版发布 将支持更多设备 Towelroot v3.0下载

    Towelroot虽然已经发布一段时间了,虽然所Towelroot可以一键ROOT很多设备,虽然它只有100多K.不过还是有一小部分的机型没办法ROOT成功的,也不知道什么原因.不过不用担心,Geoh ...

  3. 轨至轨运算放大器 rail to rail

    http://www.360doc.com/content/10/1102/16/2285160_66006645.shtml Rail to rail: 轨至轨,指器件的输入输出电压范围可以达到电源 ...

  4. Centos6.5下Oracle 11g R2安装过程

    1准备 CentOS-6.5-x86_64-bin-DVD1 linux_11gR2_database_1of2 linux_11gR2_database_2of2 VMware Workstatio ...

  5. MVC文件上传05-使用客户端jQuery-File-Upload插件和服务端Backload组件自定义上传文件夹

    在零配置情况下,文件的上传文件夹是根目录下的Files文件夹,如何自定义文件的上传文件夹呢? MVC文件上传相关兄弟篇: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小  MVC文 ...

  6. ubuntu下C++和C编程

      一.anjuta Anjuta DevStudio 的官方地址:http://anjuta.sourceforge.net/Anjuta是一个C/C++ IDE,它最大的特色是灵活,同时打开多个文 ...

  7. [翻译] MZTimerLabel 用作秒表或者倒计时

    MZTimerLabel 用作秒表或者倒计时 https://github.com/mineschan/MZTimerLabel A handy class for iOS to use UILabe ...

  8. Git 学习(三)本地仓库操作——git add & commit

    Git 学习(三)本地仓库操作——git add & commit Git 和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念.这在上文已有提及,本文具体说明什么是工作区及暂存区,以及 ...

  9. NYOJ-267 郁闷的C小加(二)

    郁闷的C小加(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考“郁闷的C小加(一)”),C小加很 ...

  10. Hash索引与B-Tree索引

    Hash索引 Hash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tree 索引需要从根节点到枝节点,最后才能访问到页节点这样多次的IO访问,所以 Hash 索引的查询效率要 ...