[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 ...
随机推荐
- MySQL Key值(PRI, UNI, MUL)的含义
PRI主键约束: UNI唯一约束: MUL可以重复. 参考:http://www.cnblogs.com/licheng/archive/2010/10/16/1852938.html
- calibre的注册表残留删除
卸载calibre后,注册表仍有残留 for /f %a in ('reg query HKEY_CLASSES_ROOT /f calibre /k') do reg delete %a /f
- Ali流量控制中间件Sentinel
原文链接: https://blog.csdn.net/u012190514/article/details/81383698 Sentinel 是什么 随着微服务的流行,服务和服务之间的稳定性变得越 ...
- android ButterKnife 点击事件没反应的解决方案
可能只添加了 implementation 'com.jakewharton:butterknife:8.8.1'而没有添加下面这行 annotationProcessor 'com.jakewhar ...
- WindowsServer2012R2开机进入CMD,关闭后黑屏问题
原因分析: 因为自己在卸载IIS的时候,不小心卸载了.net framework,系统没有了图形界面(由完整模式Full变为了核心模式core),需要重新恢复.net framework4.5. 解决 ...
- Includes() vs indexOf() in JavaScript
碰到一个问题, 部分机器网页数据源不正常, 简单排查发现是使用了较新的Array.includs 方法. 查了下兼容性, chrome 需要47版本以后支持, 客户机果然是很久的43版本. 用Arra ...
- mysql相关碎碎念
取得当天: SELECT curdate(); mysql> SELECT curdate();+------------+| curdate() |+------------+| 2013- ...
- vue里面axios使用post
let params = new URLSearchParams(); params.append('action', "login"); params.append('user' ...
- 红黑树Python实现
# coding=utf-8 # 红黑树Python实现 # 颜色常量 RED = 0 BLACK = 1 def left_rotate(tree, node): if not node.right ...
- 关于tp5自动过滤index.php
在public/.htaccess 中输入这段代码即可实现过滤index.php <IfModule mod_rewrite.c> Options +FollowSymlinks -Mul ...