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 ...
随机推荐
- 局域网ip地址扫描_v1版本
局域网ip地址扫描 工作中,我们有时需要对局域网中ip地址使用情况进行统计.可以使用shell脚本进行扫. 脚本功能: 在线使用IP写入list_online.txt文件 未在线IP写入list_of ...
- 广义Fibonacci数列模n的循环节
见这里:http://blog.csdn.net/ACdreamers/article/details/25616461 有详细的分析推理 只找出了循环节的上限,设 f[n] = (af[n - 1] ...
- 换一种方式编写 Spring MVC 接口
1. 前言 通常我们编写 Spring MVC 接口的范式是这样的: @RestController @RequestMapping("/v1/userinfo") public ...
- 2018-06-28 jq CSS处理
CSS处理 1.CSS样式 css() -> 获取jq对象的css样式 css({'':"'}) ->设置jq对象的css样式 相当于js对象的style()方法 2.位置 of ...
- Dockerfile-Namespace
Docker核心-Namespaces(命名空间) 1)概念: 命令空间是Linux内核的一个强大的特性.每个容器都有自己单独的命令空间,运行在其中的应用都是独立在操作系用中运行一样.命名空间保证了容 ...
- Anaconda3中的Jupyter notebook添加目录插件
学习python和人工智能的相关课程时安装了Anaconda3,想在Jupyter notebook中归纳整理笔记,为了方便日后查找想安装目录(Table of Contents, TOC)插件,查找 ...
- 最短路径——dijkstra算法代码(c语言)
最短路径问题 看了王道的视频,感觉云里雾里的,所以写这个博客来加深理解.(希望能在12点以前写完) 一.总体思想 dijkstra算法的主要思想就是基于贪心,找出从v开始的顶点到各个点的最短路径,做法 ...
- 笨办法学习python之hashmap
#!/user/bin/env python #-*-coding:utf-8 -*- #Author: qinjiaxi #初始化aMap列表,把列表num_buckets添加到aMap中,num_ ...
- php-fpm搜索php.ini很奇怪的一个现象
php-fpm 找不到 php.ini phpinfo 或者 php -i 上来看,搜索的目录都是/usr/local/php/etc下,但是事实上并没用去找这个目录 bin/php --ini 来看 ...
- 3.1Go变量
3.1 Go变量 变量是对内存中数据存储空间的表示,如同门牌号对应着房间,同样的,变量名字对应变量的值. 变量:本质就是一块内存空间.用于存储某个数值.该数值在运行时可以改变. 变量使用步骤 1.声明 ...