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 ...
随机推荐
- Mysql 常用函数(13)- right 函数
Mysql常用函数的汇总,可看下面系列文章 https://www.cnblogs.com/poloyy/category/1765164.html right 的作用 返回字符串 str 中最右边的 ...
- Java ThreadLocal解析
简介 ThreadLocal 类似局部变量,解决了单个线程维护自己线程内的变量值(存.取.删),让线程之间的数据进行隔离.(InheritableThreadLocal 特例) 这里涉及三个类,Thr ...
- 萌新带你开车上p站(终极番外)
本文由“合天智汇”公众号首发,作者:萌新 0x01前言 这关其实和pwn关系不大,主要考察的都是linux下一些函数的操作,考察linux的基本功.涉及到的知识点包括一些经典的函数原型.IO重定向.文 ...
- 一、HDFS 原理分析
HDFS 全称 Hadoop Distribute File System,是 Hadoop 的一个分布式文件系统 一.HDFS 的系统结构 1.1 数据块 -- block 文件在 HDFS 上分块 ...
- PAT 1006 Sign In and Sign Out (25分) 字符串比较
题目 At the beginning of every day, the first person who signs in the computer room will unlock the do ...
- 不吹牛X,我真的干掉了if-else
我们在web开发中,经常使用数据库表中的字段作为"标记"来表示多个"状态",比如: 我们就以某宝的在线购物流程为例进行分析.在订单表中,使用zt字段来表示定单的 ...
- javascript 获取页面的高度及滚动条的位置的代码
http://www.jb51.net/article/23331.htm javascript 获取页面的高度及滚动条的位置的代码 作者: 字体:[增加 减小] 类型:转载 javascript ...
- MySQL(2)— 数据库的基本操作
二.数据库 2-1.操作数据库(了解) 1.创建数据库 CREATE DATABASE [IF NOT EXIST] myDatabase; 2.删除数据库 DROP DATABASE `myData ...
- JUC整理笔记一之细说Unsafe
JUC(java.util.concurrent)的开始,可以说是从Unsafe类开始. Unsafe 简介 Unsafe在sun.misc 下,顾名思义,这是一个不安全的类,因为Unsafe类所操作 ...
- CICD:Jenkins入门和使用
最近,我们使用的开发服务器被回收了,换了一台新的服务器,CI/CD平台需要重新搭建. 我的运维能力一直薄弱,所以借此机会学习了一番如何使用Jenkins进行持续集成开发和部署,实践并踩了一些坑,在此记 ...