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. 异步加载js

    //异步加载js function loadScript(url,callback){ var script = document.createElement("script"); ...

  2. treap 1286郁闷的出纳员.cpp

    #include<cstdio>#include<cstdlib>#include<ctime>struct shu{ int l,r,sum,zhi,dui;}a ...

  3. POJ 2762 tarjan缩点+并查集+度数

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15494 ...

  4. Dictionary解析json,里面的数组放进list,并绑定到DataGridView指定列

    Dictionary解析json,1.根据json建立相应的实体类,json里面的数组形式放进list集合2.取list中的数据,将相应的数据绑定到DataGridView,如下:循环(动态添加一行数 ...

  5. 【C语言学习】-03 循环结构

    本文目录 循环结构的特点 while循环 do...while循环 for循环 回到顶部 一.循环结构的特点 程序的三种结构: 顺序结构:顺序执行语句 分支结构:通过进行一个判断在两个可选的语句序列之 ...

  6. Unity4.3.3激活

    Unity4.X Win版本的破解方法: <ignore_js_op> 1.安装unity4.X,一路按提示下一步,要断网,直到激活运行软件后再联网2.将Unity 4.x Pro Pat ...

  7. jQuery之$('#id')和$('#'+id)

    最近在项目中使用$('#id')时,发现拿到的元素怎么都是空元素,(前提是id是作为变量),纠结了好一阵,使用fire bug也调试了半天终于发现原来$('#id')是使用整体来匹配,即查找id 为i ...

  8. FragmentActivity+FragmentTabHost+Fragement instead of TabActibvity+TabHost+Activity

    http://www.tuicool.com/articles/NzeMJz http://www.tuicool.com/articles/FJ7VBb FragmentTabHost切换Fragm ...

  9. C++11 std::copy

    这个函数并不是简单的 while(first != last) { *result = *first; result++; first++; } 事实上这种写法是最具普适性的,值要求inputIter ...

  10. AFNnetworking入门

    AFNetworking官网入门教程简单翻译,学习 AFNetworking 是一个能够快速使用的ios和mac os x下的网络框架,它是构建在Foundation URL Loading Syst ...