题目描述:

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

解题思路:

从第一个开始数起,对于输入n,则数n-1次即可。

代码如下:

public class Solution {
public String countAndSay(int n) {
int i = 1;
if (n <= 0)
return null;
String s = "1";
while (i <= n - 1) {
s = countAndSay(s);
i++;
}
return s;
} public String countAndSay(String s) {
int count = 1;
StringBuffer sb = new StringBuffer();
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i - 1) == s.charAt(i))
count++;
else {
sb.append(count).append(s.charAt(i - 1));
count = 1;
}
}
sb.append(count).append(s.charAt(s.length() - 1));
return sb.toString();
}
}

Java [leetcode 38]Count and Say的更多相关文章

  1. LeetCode - 38. Count and Say

    38. Count and Say Problem's Link ------------------------------------------------------------------- ...

  2. leetcode 38 Count and Say ---java

    这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 -->   111221 --> 312211 --> ..... 1个 ...

  3. LeetCode 38 Count and Say(字符串规律输出)

    题目链接:https://leetcode.com/problems/count-and-say/?tab=Description   1—>11—>21—>1211—>111 ...

  4. [LeetCode] 38. Count and Say 计数和读法

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  5. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  6. [leetcode]38. Count and Say数数

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  7. [LeetCode] 38. Count and Say_Easy

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  8. Java [Leetcode 357]Count Numbers with Unique Digits

    题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...

  9. Leetcode 38 Count and Say 传说中的递推

    class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...

随机推荐

  1. WPF 中 TreeListView 的使用

    前段时间在项目开发中需要用 TreeListView 的功能,于是在网上狂搜一通,倒也找到了几个小例子,但还是满足不了我简单的要求,由于时间紧也只能折中凑合着用了.最近时间比较充裕,把其中的例子整理一 ...

  2. poj 2947 Widget Factory

    Widget Factory 题意:有n件装饰品,有m组信息.(1 <= n ,m<= 300)每组信息有开始的星期和结束的星期(是在mod 7范围内的)并且还包括num种装饰品的种类(1 ...

  3. Android Studio 导入第三方jar包

    1.先将AS切换到Project 2.在app-main-src下建一个libs目录,将jar包拷到里面 3.右击jar,add as Library

  4. Increase SharePoint Execution Timeout

    <system.web> <compilation batch="false" batchTimeout="600" maxBatchSize ...

  5. jQuery DataTables Plugin Meets C#

    Over the weekend, I was doing some work on the internal CMS we use over at eagleenvision.net and I w ...

  6. sql server 2012 5120错误

    把放置放置数据库的文件夹的权限更改为完全控制即可解决这个问题.

  7. 安装ADT Cannot complete the install because one or more required items could not be found.

    点击进行安装,将会弹出 错误提示是: Cannot complete the install because one or more required items could not be found ...

  8. POJ1260Pearls

    http://poj.org/problem?id=1260 题意 :这个题大概是讲,给你几种等级不同的珠宝,然后告诉你它的数量和价值,等级是升序排列的,且随等级的升高价值也随之升高,但为了防止有的客 ...

  9. 2.Adding a Controller

    MVC stands for model-view-controller.  MVC is a pattern for developing applications that are well ar ...

  10. Android:控件布局(相对布局)RelativeLayout

    RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...