LintCode之报数
来源:互联网 发布:维尔迪数据 编辑:程序博客网 时间:2024/05/22 16:52
题目来源:报数
题目描述:
报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数。如下所示:
1, 11, 21, 1211, 111221, …
1 读作 “one 1” -> 11.
11 读作 “two 1s” -> 21.
21 读作 “one 2, then one 1” -> 1211.
给定一个整数 n, 返回 第 n 个顺序。
样例:
给定 n = 5, 返回 “111221”.
Java代码
String countAndSayForOneString(String input) { char tmp = input.charAt(0); int num = 1; StringBuffer newString = new StringBuffer(""); for(int k=1;k<input.length();k++) { if(input.charAt(k)==tmp) { num++; continue; } newString.append(Integer.toString(num) + tmp); tmp = input.charAt(k); num = 1; } newString.append(Integer.toString(num) + tmp); return newString.toString(); } public String countAndSay(int n) { String result = "1"; int i = 1; while(i<n) { result = countAndSayForOneString(result); i++; } return result; }
0 0
- LintCode之报数
- LintCode:报数
- lintcode ----报数
- 报数-lintcode
- lintcode,报数
- lintcode--报数
- 报数-LintCode
- lintcode 报数
- 报数(LintCode)
- 容易 报数 (lintcode)
- lintCode(420)——报数
- lintcode python代码 420 报数
- LintCode 第420题 报数
- lintcode 报数(Count and Say) (Java)
- 报数
- 报数
- 报数
- 报数
- Spring 整合 hibernate 时,抛异常: createQuery is not valid without active transaction
- Spring4 整合 Hibernate4
- windows连虚拟机mongodb
- 如何产生已知概率密度函数的随机数?
- hdu 1242 Rescue
- LintCode之报数
- [网络流]最大流算法 Dinic
- django 注册、登录、第三方接口程序
- 对JAVASCRIPT匿名函数的理解(透彻版)
- 5-37 模拟EXCEL排序
- c++跨文件变量声明
- NYOJ 353 Dungeon Master【BFS】
- 第二章.一切都是对象之基本类型【主数据类型】数组
- Android 使用 JSON 保存应用数据