38. Count and Say

Easy

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"
package leetcode.easy;

public class CountAndSay {
@org.junit.Test
public void test() {
for (int i = 1; i <= 4; i++) {
System.out.println(countAndSay(i));
}
} public String countAndSay(int n) {
String str = "1";
for (int i = 1; i < n; i++) {
str = countID(str);
}
return str;
} private static String countID(String str) {
// TODO Auto-generated method stub
StringBuffer buffer = new StringBuffer();
char c = str.charAt(0);
int count = 1;
for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) == c) {
count++;
} else {
buffer.append(count);
buffer.append(c);
c = str.charAt(i);
count = 1;
}
}
buffer.append(count);
buffer.append(c);
return buffer.toString();
}
}

LeetCode_38. Count and Say的更多相关文章

  1. nodejs api 中文文档

    文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...

  2. C#中Length和Count的区别(个人观点)

    这篇文章将会很短...短到比你的JJ还短,当然开玩笑了.网上有说过Length和count的区别,都是很含糊的,我没有发现有 文章说得比较透彻的,所以,虽然这篇文章很短,我还是希望能留在首页,听听大家 ...

  3. [PHP源码阅读]count函数

    在PHP编程中,在遍历数组的时候经常需要先计算数组的长度作为循环结束的判断条件,而在PHP里面对数组的操作是很频繁的,因此count也算是一个常用函数,下面研究一下count函数的具体实现. 我在gi ...

  4. EntityFramework.Extended 实现 update count+=1

    在使用 EF 的时候,EntityFramework.Extended 的作用:使IQueryable<T>转换为update table set ...,这样使我们在修改实体对象的时候, ...

  5. 学习笔记 MYSQL报错注入(count()、rand()、group by)

    首先看下常见的攻击载荷,如下: select count(*),(floor(rand(0)*2))x from table group by x; 然后对于攻击载荷进行解释, floor(rand( ...

  6. count(*) 与count (字段名)的区别

    count(*) 查出来的是:结果集的总条数 count(字段名) 查出来的是: 结果集中'字段名'不为空的记录的总条数

  7. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  8. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  9. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

随机推荐

  1. UML再论关系extend和include

    我在画用例图时,图中既有extend关系也有include关系,师父就问我这两种关系的区别,我在画的时候确实查阅了很多资料,可是在问的时候还是回答不上来,这就是这篇博客得来的缘由了. [include ...

  2. Robot Framework--RIDE面板与库的说明

    Robot Framework的测试用例是以project作为单位进行管理的.一个project可以包含多个Test Suite文件,每一个Test Suite可以包含多条测试用例一个Test Sui ...

  3. 9 loader - 分析webpack调用第三方loader的过程

    注意:webpack处理第三方文件类型的过程: 1.发现这个要处理的文件不是JS文件,然后就去配置文件中,查找有没有对应的第三方loader规则 2.如果能找到对应的规则,就会调用对应的loader处 ...

  4. Linux中drwxr-xr-x.的意思和权限

    转载:https://blog.csdn.net/weixin_39209728/article/details/79729885 读(read),写(write),执行r(recute)简写即为(r ...

  5. 多继承以及MRO顺序

    class A: def test(self): print("A --- test方法") def demo(self): print("A --- demo方法&qu ...

  6. javax.persistence.TransactionRequiredException: Executing an update/delete query

    最近在springboot中整合jpa的时候碰到一个异常,异常如下 javax.persistence.TransactionRequiredException: Executing an updat ...

  7. 小程序官方基础项目api请求

    onLaunch: function () { // 展示本地存储能力 var logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now ...

  8. CMD browser in Linux -- Links

    Links is an open source web browser written in C programming Language. It is available for all major ...

  9. wepy框架入门

    安装 wepy 命令行工具. npm install wepy-cli -g 在开发目录生成开发DEMO. wepy new myproject 开发实时编译. wepy build --watch ...

  10. 原生table表格的使用

    近期公司让我修改一些之前的table标签写的页面,感觉对table相关的标签不是太熟悉,于是专门整理一下: 1.如果给td标签设置百分比宽度,比如有10列内容,我们却设置了每个单元格是30%的宽度,会 ...