38. Count and Say ★
来源:互联网 发布:摩尔软件安装 编辑:程序博客网 时间:2024/06/06 09:04
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.
题目分析:
此题使用StringBuffer构造数组,注意StringBuffer在执行函数操作后返回的仍然是该对象,需要输出数组时不要忘了使用toString函数。
题目代码:
public class Solution {
public static String countAndSay(int n) {
StringBuilder curr = new StringBuilder("1");
StringBuilder now = new StringBuilder();
for(int i=1;i<n;i++){
now = curr;
char say = now.charAt(0);
curr = new StringBuilder();
int count = 1;
for(int j=1;j<now.length();j++){
if(now.charAt(j)!=say){
curr.append(count).append(say);
count = 1;
say = now.charAt(j);
}
else count++;
}
curr.append(count).append(say);
}
return curr.toString();
}
}
- 38. Count and Say ★
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 最长公共子序列问题LCS
- 从SG函数浅谈解决博弈问题的通法
- c++ 虚函数的实现机制:笔记
- Java阻塞队列之BlockingQueue
- Spring ThreadPoolTaskExecutor线程池
- 38. Count and Say ★
- Android 基础知识05
- AngularJs轻松入门(一)创建第一个应用
- 防XSS的两个函数
- FileOperation
- 104. Maximum Depth of Binary Tree
- MySQL数据库的数据库引擎有哪些
- Search Graph Nodes
- 35. Search Insert Position