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.

代码如下:

方法一:

 public class Solution {
public String countAndSay(int n) { String s="1";
if(n==0||n==1)
return s; String result="";
for(int k=1;k<n;k++) //用for循环
{ char[] ss=s.toCharArray(); result="";
int i=0;
int j=0; for( i=0;i<ss.length;)
{
char a=ss[i];
int count=0;
for(j=i;j<ss.length;j++)
{
if(a==ss[j])
count++;
else break;
}
result=result+Integer.toString(count)+Character.toString(a); i=j;
} s=result;
} return s;
}
}

方法二:

 public class Solution {
public String countAndSay(int n) { String s="1";
if(n==0||n==1)
return s; s=countAndSay(n-1);//用递归
String result=""; char[] ss=s.toCharArray();
result="";
int i=0;
int j=0; for( i=0;i<ss.length;)
{
char a=ss[i];
int count=0;
for(j=i;j<ss.length;j++)
{
if(a==ss[j])
count++;
else break;
}
result=result+Integer.toString(count)+Character.toString(a); i=j;
} return result;
}
}

38. Count and Say的更多相关文章

  1. LeetCode - 38. Count and Say

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

  2. LeetCode题解38.Count and Say

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

  3. leetCode练题——38. Count and Say

    1.题目 38. Count and Say The count-and-say sequence is the sequence of integers with the first five te ...

  4. 【leetcode❤python】 38. Count and Say

    #-*- coding: UTF-8 -*- class Solution(object):    def countAndSay(self, n):        """ ...

  5. 【LeetCode】38 - Count and Say

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

  6. Java [leetcode 38]Count and Say

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

  7. 38. Count and Say - Unsolved

    https://leetcode.com/problems/count-and-say/#/description The count-and-say sequence is the sequence ...

  8. 【一天一道LeetCode】#38. Count and Say

    一天一道LeetCode系列 (一)题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, ...

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

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

随机推荐

  1. 关于http断点续传相关的RANGE这个header

    <?php //1.txt内容"1234567890" socketData('127.0.0.1','/1.txt',80,"RANGE:bytes=0-0\r\ ...

  2. ARM安装ROS- indigo

    Ubuntu ARM install of ROS Indigo 溪西创客小屋 There are currently builds of ROS for Ubuntu Trusty armhf. T ...

  3. 从报错“无效操作,连接被关闭”探究Transaction的Timeout超时机制

    1.报错如下:Invalid Operation the connection is closed,无效操作,连接被关闭.这个错误是并不是每次都报,只有在复杂操作.大事务的情况下才偶然报出来. sta ...

  4. python 字典排序 关于sort()、reversed()、sorted()

    一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...

  5. asp.net mvc 2.o 中使用JQuery.uploadify

    From:http://www.cnblogs.com/strugglesMen/archive/2011/07/01/2095916.html 官方网站http://www.uploadify.co ...

  6. POJ 1739

    楼教主男人八题之一... 题目大意: 求从左下角经过所有非障碍点一次到达右下角的方案数 这里不是求回路,但是我们可以考虑,在最下面一行再增加一行,那么就可以当做求此时左下角到右下角的回路总数,那么就转 ...

  7. POJ 1741 树上的点分治

    题目大意: 找到树上点对间距离不大于K的点对数 这是一道简单的练习点分治的题,注意的是为了防止点分治时出现最后分治出来一颗子树为一条直线,所以用递归的方法求出最合适的root点 #include &l ...

  8. android Xutils dbutils 注解

    xUtils DbUtils 关于实体类注解 汇总 RockyZhang 发布于 1年前,共有 0 条评论 先来官方demo DbUtils db = DbUtils.create(this);    ...

  9. sql左连接,右连接,内连接

    1.sql查询时什么叫左连接和右连接    左连接和右连接都是外部连接,也就是区别于内部连接,它对不满足连接条件的行并不是象内部连接一样将数据完全过滤掉,而是保留一部分数据,行数不会减少.    左或 ...

  10. Struts2 validate校验

    一般的,用户注册的时候,我们需要校验一些用户提交过来的参数. 一般有两道屏障,一是在前台页面上使用js进行验证,直接杜绝了不正常信息的提交.二是将提交过来的信息进行验证,不通过则返回注册页面并显示错误 ...