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=1时返回“1”,n=2时返回“11”,n=3时返回"21",n=4时返回"1211"
所以肯定是用递归也进行求解,JAVA实现如下:

	static public String countAndSay(int n) {
if(n==1)
return "1";
return countAndSay(countAndSay(n-1));
} static String countAndSay(String s){
StringBuilder sb = new StringBuilder();
int count = 0;
char temp='*';
for(int i=0;i<s.length();i++){
if(temp!=s.charAt(i)){
if(temp!='*'){
sb.append(count);
sb.append(temp);
}
count=1;
temp=s.charAt(i);
}
else
count++;
}
sb.append(count);
sb.append(temp);
return sb.toString();
}

Java for LeetCode 038 Count and Say的更多相关文章

  1. [LeetCode] 038. Count and Say (Easy) (C++/Python)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...

  2. Java for LeetCode 222 Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  3. Java for LeetCode 204 Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...

  4. LeetCode 038 Count and Say

    题目要求:Count and Say The count-and-say sequence is the sequence of integers beginning as follows:1, 11 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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. ...

  9. 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 ...

随机推荐

  1. 【前端】Sublime text3 插件LiveReload 实现实时预览

    1.首先要安装插件LiveReload Sublime text3. 菜单 preferences->packages control,输入install.. 回车,输入LiveReload回车 ...

  2. 【HDU 5438】Ponds

    题意 不断删去度数为1的点,最后求有奇数个点的联通块的权值之和. 分析 存边的时候,要头尾都存这个边.用dfs或者队列删点,再用并查集或者dfs确定联通块,然后统计联通块的点数,最后累加. 我自己写的 ...

  3. Ubuntu 及衍生版本用户如何安装 SmartGit/HG

    http://www.linuxidc.com/Linux/2014-06/102621.htm Ubuntu 及衍生版本用户如何安装 SmartGit/HG 6.0.0 [日期:2014-06-03 ...

  4. 【poj3159】 Candies

    http://poj.org/problem?id=3159 (题目链接) 题意 有n个小朋友,班长要给每个小朋友发糖果.m种限制条件,小朋友A不允许小朋友B比自己多C个糖果.问第n个小朋友最多比第1 ...

  5. [NOIP2011] 提高组 洛谷P1314 聪明的质监员

    题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每个矿石都有自己的重量 wi 以及价值vi .检验矿产的流程是: 1 .给定m 个区间[L ...

  6. debian , ubuntu 截取下拉菜单

    普通情况下print键很好用的,但是,截下拉菜单的时候,就怎么按都没反应了...然后换了shutter也不行,只能在静态界面截图.所以...然后就有了下面方法. 如果不是gnome怎么办?这个担心显然 ...

  7. 从注册流程 分析如何安全退出多个Activity 多种方式(附DEMO)

      退出Activity注册Android遍历   目录(?)[+] 前言 知识结构 具体方案 方案1 方法采用FLAG_ACTIVITY_CLEAR_TOP退出整个程序多activity 方案2 方 ...

  8. 字符串匹配的Boyer-Moore算法 详解 加 C# 实现

    上一篇文章,我介绍了KMP算法. 但是,它并不是效率最高的算法,实际采用并不多.各种文本编辑器的"查找"功能(Ctrl+F),大多采用Boyer-Moore算法. Boyer-Mo ...

  9. 自定义NSLog无时间

    #define SXLog(FORMAT, ...) fprintf(stderr,"file --\t%s\nline --\t%d\nmethd --\t%s\noutput --\t\ ...

  10. serialization机制

    首先说明一下序列化的知识: java中的序列化(serialization)机制能够将一个实例对象的状态信息写入到一个字节流中,使其可以通过socket进行传输.或者持久化存储到数据库或文件系统中:然 ...