LeetCode & Q38-Count and Say-Easy
String
Description:
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
1is read off as"one 1"or11.
11is read off as"two 1s"or21.
21is read off as"one 2, thenone 1"or1211.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"
个人觉得这个题意实在是反人类!!!
在此说明一下题意:
例如,5对应111221,6的结果就是对应读5得到的,也就是3个1、2个2、1个1,即:312211
my Solution:
public class Solution {
public String countAndSay(int n) {
StringBuffer sb = new StringBuffer("1");
for (int i = 1; i < n; i++) {
StringBuffer next = new StringBuffer();
int count = 1;
for (int j = 0; j < sb.length(); j++) {
if (j+1 < sb.length() && sb.charAt(j) == sb.charAt(j+1)) {
count++;
} else {
next.append(count).append(sb.charAt(j));
count = 1;
}
}
sb = next;
}
return sb.toString();
}
}
LeetCode & Q38-Count and Say-Easy的更多相关文章
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...
- 【leetcode】Count Primes(easy)
Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- 【leetcode】Count and Say (easy)
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- (easy)LeetCode 204.Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...
- Leetcode解题思路总结(Easy篇)
终于刷完了leetcode的前250道题的easy篇.好吧,其实也就60多道题,但是其中的套路还是值得被记录的. 至于全部code,请移步github,题目大部分采用python3,小部分使用C,如有 ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- leetcode@ [327] Count of Range Sum (Binary Search)
https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...
随机推荐
- python—day02
python的版本与基本类型... 第一: 讲了计算机的基础的补充,讲解了什么是操作系统,计算机硬件,应用程序之间的关系: 操作系统是一个能协调管理计算机软件与硬件的软件程序: 能帮我们发送指令集到C ...
- Lintcode245 Subtree solution 题解
[题目描述] You have two every large binary trees:T1, with millions of nodes, and T2, with hundreds of no ...
- CentOS7.4安装MySQL踩坑记录
CentOS7.4安装MySQL踩坑记录 time: 2018.3.19 CentOS7.4安装MySQL时网上的文档虽然多但是不靠谱的也多, 可能因为版本与时间的问题, 所以记录下自己踩坑的过程, ...
- 学习python之路_入门篇A
偶尔经同事的介绍进入了金角大王的博客里,看到大王编写的文章都是关于python编程的,由于自己一直也是做软件测试方面的工作,也一直想往自动化测试方面发展,了解到利用python可以进行自动化测试操作, ...
- python全栈开发-Day5 元组、字典
python全栈开发-Day5 元组.字典 一.前言 首先,不管学习什么数据类型,我们都带着以下几个问题展开学习: #1:基本使用 1 .用途 2 .定义方式 3.常用操作+内置的方法 #2:该类型 ...
- 如何 创建一个model对象保存到LIST集合里面并取出来
/// <summary> /// 缓存客服集合信息 /// </summary> public class model { /// <summary> /// 客 ...
- Elasticsearch就这么简单
一.前言 最近有点想弄一个站内搜索的功能,之前学过了Lucene,后来又听过Solr这个名词.接着在了解全文搜索的时候就发现了Elasticsearch这个,他也是以Lucene为基础的. 我去搜了几 ...
- cloneNode和replaceChild
node.cloneNode(deep) var node=document.getElementById("myList2").lastChild.cloneNode(true) ...
- 关于html表单的disabled属性的设置问题
首先,我的看法是无论disable的值是否有值,只要设置了disabled属性的表单,无论是否有值,无论值为什么,都会被禁用. 来看下面例子: 在一个群里有人说因为直接写在表单属性上是字符串,因为 ...
- 【HTML】 HTML基础知识 表单
html 表单 表单的标签是<form>,用于给网站的后台提交数据.提交的数据格式原本是什么样不太清楚,以python的flask框架来看,我从表单中得到的数据是一个字典(flask.re ...