首页 > 其他分享 >#yyds干货盘点# LeetCode面试题:外观数列

#yyds干货盘点# LeetCode面试题:外观数列

时间:2023-03-04 19:32:13浏览次数:44  
标签:11 yyds 面试题 String charAt index countAndSay LeetCode 描述

1.简述:

给定一个正整数 n ,输出外观数列的第 n 项。

「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。

你可以将其视作是由递归公式定义的数字字符串序列:

countAndSay(1) = "1"

countAndSay(n) 是对 countAndSay(n-1) 的描述,然后转换成另一个数字字符串。

前五项如下:

1.     1

2.     11

3.     21

4.     1211

5.     111221

第一项是数字 1

描述前一项,这个数是 1 即 “ 一 个 1 ”,记作 "11"

描述前一项,这个数是 11 即 “ 二 个 1 ” ,记作 "21"

描述前一项,这个数是 21 即 “ 一 个 2 + 一 个 1 ” ,记作 "1211"

描述前一项,这个数是 1211 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ,记作 "111221"

要 描述 一个数字字符串,首先要将字符串分割为 最小 数量的组,每个组都由连续的最多 相同字符 组成。然后对于每个组,先描述字符的数量,然后描述字符,形成一个描述组。要将描述转换为数字字符串,先将每组中的字符数量用数字替换,再将所有描述组连接起来。

例如,数字字符串 "3322251" 的描述如下图:

 

示例 1:

输入:n = 1

输出:"1"

解释:这是一个基本样例。

示例 2:

输入:n = 4

输出:"1211"

解释:

countAndSay(1) = "1"

countAndSay(2) = 读 "1" = 一 个 1 = "11"

countAndSay(3) = 读 "11" = 二 个 1 = "21"

countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"

2.代码实现:

class Solution {
public String countAndSay(int n) {
if(n >=2){
//递归,拿到上一轮的String
String s = countAndSay(--n);
//return 拿到本轮String
return getresult(s);
}else{
return "1";
}
}
//拿到下一个阶段的String 方法
public String getresult(String s) {
StringBuilder res = new StringBuilder();
int index = 0;
while (index < s.length()) {
if (index + 1 < s.length() && s.charAt(index) != s.charAt(index + 1)) {
res.append("1").append(s.charAt(index));
index++;
} else {
int count = 1;
while (index + 1 < s.length() && s.charAt(index) == s.charAt(index + 1)) {
index++;
count++;
}
res.append(count).append(s.charAt(index));
index++;
}
}
return res.toString();
}
}

标签:11,yyds,面试题,String,charAt,index,countAndSay,LeetCode,描述
From: https://blog.51cto.com/u_15488507/6100410

相关文章

  • leetcode-1154-easy
    DayoftheYearGivenastringdaterepresentingaGregoriancalendardateformattedasYYYY-MM-DD,returnthedaynumberoftheyear.Example1:Input:date......
  • leetcode-1185-easy
    DayoftheWeekGivenadate,returnthecorrespondingdayoftheweekforthatdate.Theinputisgivenasthreeintegersrepresentingtheday,monthandyea......
  • leetcode-1528-easy
    ShuffleStringYouaregivenastringsandanintegerarrayindicesofthesamelength.Thestringswillbeshuffledsuchthatthecharacterattheithposi......
  • leetcode-1470-easy
    ShuffletheArrayGiventhearraynumsconsistingof2nelementsintheform[x1,x2,...,xn,y1,y2,...,yn].Returnthearrayintheform[x1,y1,x2,y2,...,xn,yn]......
  • LEETCODE 982. 按位与为零的三元组
    这题暴力的话会超时,考虑用哈希表来存储前两位与的结果的数量然后在另一个循环中枚举第三位和哈希表每个下标相与,找到结果为0的,对应的哈希表值加入ans中classSolution{publ......
  • #yyds干货盘点#nginx正则表达式
    nginx正则匹配说明:细则:前缀匹配优先其中,最长匹配优先而后,按配置顺序匹配正则,正则第一个匹配到后终止无正则匹配,则用前面记住的前缀"="精确匹配,匹配后终止"~"区分大小写匹......
  • #yyds干货盘点#nginx的root 与 alias
    nginx指定文件路径有两种方式root和alias,主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。它们的使用方法和作......
  • #yyds干货盘点# LeetCode程序员面试金典:水域大小
    题目:你有一个用于表示一片土地的整数矩阵land,该矩阵中每个点的值代表对应地点的海拔高度。若值为0则表示水域。由垂直、水平或对角连接的水域为池塘。池塘的大小是指相连接......
  • #yyds干货盘点# LeetCode面试题:解数独
    1.简述:编写一个程序,通过填充空格来解决数独问题。数独的解法需遵循如下规则:数字 1-9 在每一行只能出现一次。数字 1-9 在每一列只能出现一次。数字 1-9 在每一个以粗......
  • Python全栈面试题及知识点总结
    Python全栈面试题Python全栈阶段总结:https://github.com/HkwJsxl/PythonFullStack/tree/master/NotesPython基础基础逻辑运算v2="wupeiqi"and"alex"#第一步:将a......