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. [Linux] tmux 终端复用命令行工具

    tmux 终端复用命令行工具 tmux 是一款终端复用命令行工具,通常用于 Terminal 的窗口管理.可以在终端软件重启后通过命令行恢复上次的 session. 安装运行 macOS 上使用 Ho ...

  2. Kernel Newbies内核开发新手的资源

    Jessica McKellar在Ksplice blog上的博客文章 <Linux Device Drivers> 如果你在写一个操作系统,OSDev wiki是一个不错的网站 Kern ...

  3. 各种SSD SMART 信息 转

    intel SSD Toolbox SMART信息 解释:03 – Spin Up Time (磁头加载时间)The average time it takes the spindle to spin ...

  4. centos7 通过kvm+vnc 实现远程桌面虚拟化和创建windows、Linux虚拟机

    感谢朋友支持本博客.欢迎共同探讨交流,因为能力和时间有限.错误之处在所难免,欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  5. 用Qemu模拟vexpress-a9 (一) --- 搭建Linux kernel调试环境

    参考: http://blog.csdn.net/linyt/article/details/42504975 环境介绍: Win7 64 + Vmware 11 + ubuntu14.04 32 u ...

  6. failed to get the task for process XXX(解决方案)

    引人: iOS真机调试程序,报如下错误信息: failed to get the task for process XXX 原因: 证书问题,project和targets的证书都必须是开发证书,AD ...

  7. 别忽视UIImage中的方向属性, imageOrientation-转

    转 : 别忽视UIImage中的方向属性, imageOrientation

  8. Arcgis Runtime for andriod 100 加载geodatabase

    private void LoadMY(){ try { String mainGeodatabaseFilePath = YLPub.getMapData() + "/gismap/sl. ...

  9. python笔记2-冒泡排序

    前言 面试的时候经常有面试官喜欢问如何进行冒泡排序?这个问题相信能难倒一批英雄好汉,本篇就详细讲解如何用python进行冒泡排序. 一.基本原理 1.概念: 冒泡排序(Bubble Sort),是一种 ...

  10. 自用广告过滤规则,整合xwhyc大大的,非常小才79K

    xwhyc大大 好久没更新了,自己弄了一点 更新: $third-party选项过滤多个站点的第三方广告:dy1000.com.yatu.tv,greasyfork.org 主流视频站点,请配合我的脚 ...