[leetcode]Count and Say
来源:互联网 发布:淘宝评价多久过期 编辑:程序博客网 时间:2024/05/17 23:51
问题描述:
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"
or11
.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 Count_and_Say { //javapublic String countAndSay(int n) { if(n < 1) return ""; if(n == 1) return "1";String result = "1";for(int i = 2; i <=n; i++){result = generateStr(result);}return result; }public String generateStr(String s){String tmp = "";int count = 1;char ch = s.charAt(0);for(int i = 1; i<s.length(); i++){if(ch == s.charAt(i))count++;else{tmp += count +""+ ch;count = 1;ch = s.charAt(i);}}tmp += count + "" +ch; // can't ignore the "";return tmp;}public static void main(String [] args){Count_and_Say cas = new Count_and_Say();System.out.println(cas.countAndSay(4));}}
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
- Linux网络编程之socket:unix域套接字编程与socketpair函数
- android StringBuffer类的使用
- ViewController的生命周期分析和使用
- matlab中的ezsurf函数
- Delphi中指针
- [leetcode]Count and Say
- 我们都是好孩子
- SingleTask的Activity启动模式遇到的一个奇怪现象
- cmd命令 拷贝某目录及其子目录文件到其他目录
- 各种 cast dynamic,static ,const,reinterpret
- 将一个n元一维向量向左旋转i个位置。例如,当n = 8且i = 3时,向量abcdefgh旋转为defghabc
- PAUL VIOLA 鲁棒的实时人脸检测:Robust Real-Time Face Detection
- dom4j读写xml文件
- ubuntu配置安装Qt4.8.4 同时配置openssl和QtWebkit