LeetCode 38. Count and Say(java)

原创 2018年02月05日 09:16:36

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
    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 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"
用归纳法的思想,假设我们已经得到了n-1的res,然后我们去得到n的res,在得到n的res的过程中,我们需要假设0 ~ i-1已经完成,那么i的时候,分情况判断,res.charAt(i) == res.charAt(i-1)和res.charAt(i) != res.charAt(i - 1). 注意想清楚i = 0,i = 1, i = 2之间的关系。由于要得到第n个,我们需要求出从1~n,另外每求一个1~n之间的数,都要循环从0 ~ s.length() - 1。
public String countAndSay(int n) {
        //special case
        if (n <= 0) return "";
        if (n == 1) return "1";
        //base case
        StringBuilder s = new StringBuilder("1");
        //general case
        for (int i = 2; i <= n; i++) {
            StringBuilder sb = new StringBuilder();
            int count = 1;
            for (int j = 1; j < s.length(); j++) {
                if (s.charAt(j) == s.charAt(j - 1)) {
                    count++;
                } else {
                    sb.append(count);
                    sb.append(s.charAt(j - 1));
                    count = 1;
                }
            }
            sb.append(count);
            sb.append(s.charAt(s.length() - 1));
            s = sb;
        }           
        return s.toString();
    }

LeetCode 38 : Count and Say (Java)

解题思路:我表示这道题对我来说关键就是理解题意。。。真的没理解题意,网上查了才恍然大悟。首先说一下题意。n=1时输出字符串1;n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,所以输出11;...
  • changetocs
  • changetocs
  • 2015-12-02 22:50:52
  • 1236

LeetCode 38 Count and Say(C,C++,Java,Python)

Problem: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 2...
  • runningtortoises
  • runningtortoises
  • 2015-05-19 21:36:24
  • 2436

LeetCode --- 38. Count and Say

题目链接:Count and Say The count-and-say sequence is the sequence of integers beginning as follows: ...
  • makuiyu
  • makuiyu
  • 2015-02-05 23:33:41
  • 2938

leetCode 38.Count and Say (计数和发言) 解题思路和方法

Count and Say  The count-and-say sequence is the sequence of integers beginning as follows: 1, 1...
  • xygy8860
  • xygy8860
  • 2015-07-09 20:40:16
  • 4161

【LeetCode with Python】 Count and Say

The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221...
  • nerv3x3
  • nerv3x3
  • 2014-09-21 17:38:23
  • 9515

Count and Say @python 题解

Count and Say python @题解
  • xiaolewennofollow
  • xiaolewennofollow
  • 2015-04-23 10:03:06
  • 1369

LeetCode 38 Count and Say(计数与报数)

翻译计数报数序列按如下规律开始递增: 1,11,21,1211,111221,……1 读作“1个1”或11. 11 读作“2个1”或21. 21 读作“1个2,1个1”或1211.给定一个整数n,生成...
  • NoMasp
  • NoMasp
  • 2015-12-03 10:32:47
  • 4451

leetcode 38 Count and Say C++

三个循环,总感觉很慢。。。 string countAndSay(int n) { if(!n) return ""; string str = "1"; ...
  • a2331046
  • a2331046
  • 2016-06-08 10:28:59
  • 410

LeetCode38 Count and Say(C语言解决)

题目要求: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211...
  • sinat_32547403
  • sinat_32547403
  • 2016-11-15 12:13:21
  • 489
收藏助手
不良信息举报
您举报文章:LeetCode 38. Count and Say(java)
举报原因:
原因补充:

(最多只允许输入30个字)