[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下载以及安装
因为xampp怎么都连接不上mysql,我感觉有可能是因为装mysql的时候试了很多次才安装成功,之前的mysql没有卸载干净造成的,今天把mysql卸载干净,又重新安装配置环境,但是还是连接不上,然 ...
- jQuery基础(二)DOM
DOM节点的创建 jQuery节点创建与属性的处理 创建元素节点: $("<div></div>") 创建为文本节点: $("<div> ...
- 如何在hanlp词典中手动添加未登录词
我们在使用hanlp词典进行分词的时候,难免会出现分词不准确的情况,原因是由于内置词典中并没有收录当前的这个词,也就是我们所说的未登录词,只要把这个词加入到内置词典中就可以解决类似问题,如何操作,下 ...
- .Net MVC TextBoxFor 扩展 placeholder 与 class 属性
namespace System.Web.Mvc { public static class HtmlHelperExtensions { public static MvcHtmlString Bs ...
- dependency walker检查dll依赖关系目录设置的问题
废话少说,直接上图 图中来看,似乎IESHIMS.DLL文件不存在报错,实际是因为没有加载IESHIMS.DLL所在的路径. 在我的电脑里面搜索有两个同名的dll,一个是32位的,一个是64位的. C ...
- Unity Shader Graph(一)初次尝试
软件环境 Unity Version: 2018.1.2f1 边缘发光材质效果 创建工程 打开Unity并创建一个新工程 安装依赖项 Window -> Package Manager打开包管理 ...
- 数据访问安全--数据库遮罩及断词 Data Masking & Tokenization
现在大数据时代几乎无隐私,各政府部门各公司都要求实名制(动不动手机认证,身份证号码认证),但又无力确保数据安全,称为乱象. 其实在2011年,我们就接触过数据库遮罩断词产品,一个澳大利亚公司产品. 简 ...
- php多维数组排序
数组 array(11) { [0] => array(7) { ["food_id"] => string(2) "31" ["food ...
- ORACLE_11G归档空间满,由于数据库装完后使用的是默认空间是闪回区
1.首先根据alert跟踪日志发现归档空间满,路径大致如下:cd $ORACLE_BASE/diag/rdbms/jsswgsjk/jsswgsjk1/tracetail -f alert_jsswg ...
- Apache Struts2高危漏洞(S2-057CVE-2018-11776)
花了两天时间,特此记录 一:背景: 2018年8月22日,Apache Strust2发布最新安全公告,Apache Struts2存在远程代码执行的高危漏洞. 二:漏洞产生原理: 1.需要知道对应跳 ...