LeetCode 038 Count and Say
2015-11-10 09:51
302 查看
题目描述
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.
代码
[code] public String countAndSay(int n) { String init = "1"; for (int i = 1; i < n; i++) { init = countAndSay(init); } return init; } String countAndSay(String string) { char[] str = string.toCharArray(); String s = ""; int p = 1; int count = 1; char last = str[0]; for (; p < str.length; p++) { if (str[p] == last) { count++; } else { s += "" + count + last; count = 1; last = str[p]; } } s += "" + count + last; return s; }
相关文章推荐
- 设置form中表单内容边框不显示
- 用朴素贝叶斯对wine数据集分类
- SAP ABAP收货或者货物移动(MIGO,MB11,MB1A)在保存时候的增强点
- setDefaultUncaughtExceptionHandler
- TextView换行对齐
- JAVA操作csv文件(导入导出)
- linux下c/c++实例之八字符串分隔、去空格和转大小写
- java 随机生成数字和密码
- DBAplus广州社群第一次线下活动
- DBAplus广州社群第一次线下活动
- Software update notification template (软件更新通知模板)
- Callable,Runnable比较及用法
- java泛型见解
- Windows下安装并设置Redis
- mysql 锁 for update
- iOS9下App Store新应用提审攻略
- (总结)隐藏Apache版本号的必要性与方法
- strcpy和memcpy
- 用Python写了一个数据格式转换小程序
- 倒计时显示时间,显示毫秒