[leetcode] Count and Say
来源:互联网 发布:lua map数组 编辑:程序博客网 时间:2024/05/14 04:39
Count and Say
Total Accepted: 29451 Total Submissions: 114094My SubmissionsThe 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.
package com.wyt.leetcodeOJ;/** * @author wangyitao * @Date 2015-01-23 * @version * @Description 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 CountandSay {public static void main(String[] args) {System.out.println(countAndSay(6));}public static String countAndSay(int n) {String str = "1";if (n == 1) {return str;}for (int i = 2; i <= n; i++) {str = getTmp(str);}return str;}public static String getTmp(String nstr) {String str = "";char tmp;tmp = nstr.charAt(0);int count = 0;for (int i = 0; i < nstr.length(); i++) {if (nstr.charAt(i) == tmp) {count++;} else {str += count;str += tmp;tmp = nstr.charAt(i);count = 1;}}str += count;str += tmp;return str;}}
0 0
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- [LeetCode]Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- LeetCode: Count and Say
- LeetCode - Count and Say
- [LeetCode] Count and Say
- Count and Say 【leetcode】
- [LeetCode] Count and Say
- Leetcode Count and Say
- LeetCode Count and Say
- LeetCode | Count and Say
- JVM调优总结
- 浅谈DAS、SAN、NAS
- [codevs 1907] 方格取数3
- POJ 3026 Borg Maze(kuangbin带你飞 专题六:最小生成树,完结)
- 垃圾回收器
- [leetcode] Count and Say
- jackson将java对象转换为json字符串
- apache2虚拟主机配置
- Android 平台简介
- Scala初探(三)
- Python核心编程 第十章
- Java线程面试题 Top 50 (转载)
- 深入linux内核架构-第二章-进程管理和调度
- EularProject 64: 平方根的周期性