[leetcode]38. Count and Say数数
The count-and-say sequence is the sequence of integers with the first five terms as following:
1. 1
2. 11
3. 21
4. 1211
5. 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 term of the count-and-say sequence.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
Input: 1
Output: "1"
Example 2:
Input: 4
Output: "1211"
题意:
给定一个数字串,读出来。并把读的方法也表示成一个数字串。如此计算出第N个串。
1)、1
2)、11,表示1)有1个1,组合起来就是11。
3)、21,表示2)有2个1,组合起来就是21。
4)、1211,表示3)有1个2,2个1,组合起来就是1211。
5)、111221,表示4)有1个1,1个2,2个1,组合起来就是111221。
6)、312211,表示5)有3个1,2个2,2个1,组合起来就是312211。
以此类推,求给定n行的输出结果。
思路:
"1 1 1 2 2 1"
j 查看 j 对应值 == j-1 对应值, count更新为2
j 查看 j 对应值 == j-1 对应值, count更新为3
j 查看 j 对应值 != j-1 对应值,打包前面(count: 3) + previous.charAt(j-1) = '3' + '1' = '31', count初始化为1
j 查看 j 对应值 == j-1 对应值, count更新为2
j 查看 j 对应值 != j-1 对应值,打包前面(count: 2) + previous.charAt(j-1) = '2' + '2' = '22', count初始化为1
代码:
class Solution {
public String countAndSay(int n) {
String pre = "1";
for(int i = 2; i<=n; i++){
StringBuilder sb = new StringBuilder();
int count = 1;
for(int j =1; j< pre.length() ; j++){
if(pre.charAt(j) == pre. charAt(j-1)){
count++;
}else{
sb.append(count+"");
sb.append(pre.charAt(j-1));
count = 1;
}
}
sb.append(count+"");
sb.append(pre.charAt(pre.length()-1));
pre = sb.toString();
}
return pre;
}
}
[leetcode]38. Count and Say数数的更多相关文章
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- Java [leetcode 38]Count and Say
题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...
- LeetCode 38 Count and Say(字符串规律输出)
题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111 ...
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- [LeetCode] 38. Count and Say_Easy
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Leetcode 38 Count and Say 传说中的递推
class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...
- leetcode 38 Count and Say ---java
这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 --> 111221 --> 312211 --> ..... 1个 ...
- LeetCode - 38. Count and Say(36ms)
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- [LeetCode] 248. Strobogrammatic Number III 对称数III
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
随机推荐
- django 生产环境部署建议
参考django官方建议 一种优秀的作法是使用前缀/ws/来区分WebSocket连接和普通HTTP连接,以便修改配置后,使Channels更容易部署到生产环境中. 特别是对于大型站点,可以配置像ng ...
- python3中的编码
python2字符串编码存在的问题: 使用 ASCII 码作为默认编码方式,对中文处理不友好 把字符串分为 unicode 和 str 两种类型,将unicode作为唯一内码,误导开发者 python ...
- 关于set_input_delay的用法分析
关于set_input_delay的用法分析 数据分为了系统同步和源同步: 对于下降沿采集数据的情况,当下降沿时钟延迟dv_afe到达无效数据最左端时,图中1位置,为最小延时,即采集不到有效数据的临界 ...
- Redis 存储数组
我们知道Redis是不可以直接存储数组的. 我们只需在存储数组之前序列化(serialize)一下, 然后获取的时候反序列化(unserialize) 就解决这个问题了!
- JAVA多线程17个问题
1.Thread 类中的start() 和 run() 方法有什么区别? Thread.start()方法(native)启动线程,使之进入就绪状态,当cpu分配时间该线程时,由JVM调度执行run( ...
- gentoo 图像方面的软件
图像方面的软件一般包括:查看图像,屏幕截图,图像修改. 查看图像简单的可以安装 feh,但是 feh 一般作为墙纸来用.稍微好一些的是 gqview. 屏幕截图可以用 screengrab,使用的时候 ...
- 最强Hibernate搭建文章(转)
Hibernate优势: 1.Hibernate对JDBC访问数据库的代码做了轻量级的封装,大大简化了数据访问的层的重复性代码,并却减少了内存消耗,加快了运行效率. 2.Hibernate是一个基于J ...
- 《算法导论》——计数排序Counting Sort
今天贴出的算法是计数排序Counting Sort.在经过一番挣扎之前,我很纠结,今天这个算法在一些scenarios,并不是最优的算法.最坏情况和最好情况下,时间复杂度差距很大. 代码Countin ...
- Jquery中addClass方法不起作用的解决方案
selected类是要在点击后添加上去的新样式,在点击后,发现没有变化,打开开发者工具,发现selected类已经添加成功了. 在这里没有显示成功的主要原因是后添加的样式表优先级更低,我暂时不清楚具体 ...
- python 列表复制给另一个列表,改值两个列表均会改变(备忘)
http://blog.csdn.net/lc_lc2000/article/details/53135839 本意是使A = B,B为一个列表,结果在后续对A的操作中,导致B中的值也改变了,才回忆起 ...