Java实现 LeetCode 38 外观数列
38. 外观数列
「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。前五项如下:
1
11
21
1211
111221
1 被读作 “one 1” (“一个一”) , 即 11。
11 被读作 “two 1s” (“两个一”), 即 21。
21 被读作 “one 2”, “one 1” (“一个二” , “一个一”) , 即 1211。
给定一个正整数 n(1 ≤ n ≤ 30),输出外观数列的第 n 项。
注意:整数序列中的每一项将表示为一个字符串。
示例 1:
输入: 1
输出: “1”
解释:这是一个基本样例。
示例 2:
输入: 4
输出: “1211”
解释:当 n = 3 时,序列是 “21”,其中我们有 “2” 和 “1” 两组,“2” 可以读作 “12”,也就是出现频次 = 1 而 值 = 2;类似 “1” 可以读作 “11”。所以答案是 “12” 和 “11” 组合在一起,也就是 “1211”。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/count-and-say
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public String countAndSay(int n) {
String pre = "1";
for(int i=1; i<n; i++) {
StringBuilder temp = new StringBuilder();
char c = pre.charAt(0);
int cnt = 1;
for(int j=1; j<pre.length(); j++) {
char cc = pre.charAt(j);
if(c == cc) {
cnt++;
} else {
temp.append(cnt).append(c);
cnt = 1;
c = cc;
}
}
temp.append(cnt).append(c);
pre = temp.toString();
}
return pre;
}
}
Java实现 LeetCode 38 外观数列的更多相关文章
- 【LeetCode】38. 外观数列 Count and Say
作者: 负雪明烛 id: fuxuemingzhu 公众号:负雪明烛 本文关键词:LeetCode,力扣,算法,算法题,外观数列,Count and Say,刷题群 目录 题目描述 题目大意 解题方法 ...
- PAT(B) 1084 外观数列(Java)
题目链接:1084 外观数列 (20 point(s)) 题目描述 外观数列是指具有以下特点的整数序列: d, d1, d111, d113, d11231, d112213111, - 它从不等于 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- 帝国cms 批量替换 字段内容包含的 指定的 关键字 SQL命令
帝国cms 批量替换 字段内容包含的 指定的 关键字update phome_ecms_news_data_1 set newstext=replace(newstext,'原来','现在');
- 反射,获取类的属性以及get方法
vo实体类: public class Result { /*** * 返回结果code * ok:10000 * error:20000 */ private String code; /*** * ...
- React之Antd table表格渲染按钮问题
问题描述:table表格渲染表格数据时,会自动触发操作列中Button的onClick函数,表格渲染完成后,点击Button按钮,onClick函数不能被触发. // 定义表格表头数据 问题写法: ...
- RobotFramework Selenium2Library 关键字详解
*** Settings *** Library Selenium2Library *** Keywords *** Checkbox应该不被选择 [Arguments] ${locator} Che ...
- mysql和oracle 关于多表join的区别
http://stackoverflow.com/questions/10953143/join-performance-oracle-vs-mysql 翻译自上面的链接. Given a query ...
- 【雕爷学编程】Arduino动手做(44)---类比霍尔传感器
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...
- vue中事件代理
由于在vue实例内部,this指向了实例本身,所以在编写事件代理的时候,用e.currentTarget引用绑定事件代理的元素,e.target引用事件目标元素.刚刚不注意想用this引用代理元素报错 ...
- rasdaman介绍及安装
一.分布式介绍 Rasdaman中的主节点称为Rasdaman的主机,它充当中央Rasdaman请求分派器并且控制所有服务器进程.Rasdaman管理器接收客户机请求并将这些请求分配给服务器进程.服务 ...
- POJ1436
题目链接:https://vjudge.net/problem/POJ-1436 解题思路:基于y轴建立线段树. 如图是根据样例画出的图.下面都以题目样例为例. 但是,如果仅仅以给出的y1, y2为边 ...
- SPL基础接口
Iterator 迭代器接口 SPL规定,所有实现了Iterator接口的class,都可以用在foreach Loop中.Iterator接口中包含5个必须实现的方法: interface Iter ...