【leetcode】【38】Count and Say
来源:互联网 发布:软件开发工程师怎么样 编辑:程序博客网 时间:2024/06/05 23:54
一、问题描述
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.
二、问题分析
起始sequence为1,读作“one 1”那么下一个sequence为11,而11读作“two 1s”那么下一个sequence为21,以此类推。我们可以发现下一个sequence依赖与上一个sequence,而下一次操作下一个sequence又会作为上一个sequence出现,即循环。
三、Java AC代码
public String countAndSay(int n) { if (n == 0) {return "";}String res = "1";char ch;int count;for(int i=1;i<n;++i){StringBuilder sb= new StringBuilder();count = 0;ch = res.charAt(0);for(int j=0;j<res.length();j++){if (res.charAt(j) == ch) {count++;} else {sb.append(count);sb.append(ch);ch = res.charAt(j);count = 1;}}sb.append(count);sb.append(ch);res = sb.toString();}return res; }
0 0
- [LeetCode 38] Count and Say
- LeetCode(38) Count and Say
- [leetcode 38] Count and Say
- [Leetcode] 38 - Count and Say
- leetcode-38 count and say
- [LeetCode 38]Count and Say
- LeetCode 38 Count and Say
- leetcode-38 Count and Say
- Leetcode 38 Count and Say
- Leetcode---(38)Count and Say
- leetcode 38: Count and Say
- Leetcode# 38 Count and Say
- leetcode[38]:Count and Say
- leetcode-38 Count And Say
- [Leetcode]#38 Count and Say
- LeetCode 38 Count and Say
- leetcode 38:Count and Say
- [LeetCode-38] Count and Say
- 使用jQuery修改伪属性的样式,dorado中自定义弹出框的图标
- Android蓝牙开发介绍
- Windows驱动开发之准备篇
- 【算法分析】排序算法:希尔、归并、快速、堆排序
- ThreadPool&AsyncTask
- 【leetcode】【38】Count and Say
- android BaseAdapter 自定义适配器 BaseAdapter基类抽取 二
- 用属性动画简简单单实现android导航栏特效
- Tomcat7集群共享Session 基于redis进行统一管理
- JAVA对象和类
- C语言数据类型(一)
- Android AIDL 初始学习
- android selector的用法解析
- n阶勒让德