[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 ...
随机推荐
- 将文件夹下的所有csv文件存入数据库
# 股票的多因子分层回测代码实现 import os import pymysql # import datetime, time # from config import * database_ta ...
- .Net Core 项目在Windows服务中托管【转载】
本文以创建的WebAPI项目为例子进行讲解(本人使用VS Code创建的项目) 1.使用VS Code创建WebAPI项目(项目名称自定义) 2.在创建的项目csproj项目文件中,确认是否存在运行时 ...
- 对于HTML页面中CSS, JS, HTML的加载与执行过程的简单分析
来自 https://blog.csdn.net/u011088260/article/details/79563315 最近在研究HTML页面中JavaScript的执行顺序问题.在Java ...
- PowerDesiger 生成C#实体类,字段转变成大小写方法
1.打开编辑生成C#实体类的PD脚本 2..L: 表示全部转小写,.U: 表示全部转成大写.
- python网页爬虫开发之三
1.抓取目录页后用lxml进行页面解析,获取抓取列表 python3.6 urlparse模块变为urllib.parse 2.Python中有一个专门生成各类假数据的库:Faker 3.python ...
- U3D学习13-数据存储
1.SQLLite 要注意Plguins的三个dll资源 2.利用ScriptableObject将数据存储为资源(小规模数据) using UnityEngine; using System.Col ...
- python常见循环练习
第一题:求5的阶乘 # 方法1,递归 def jc(num): if num == 1: return 1 else: return num*jc(num-1) print(jc(5)) # 方法2, ...
- Requests将verify设置为False后取消警告的方式
方法一 import requests import urllib3 urllib3.disable_warnings() resp = requests.get('https://www.***.c ...
- 204. jetcache:在Spring Boot中怎么玩?
[视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源 ...
- spring梳理