1—>11—>21—>1211—>111221—>312211—>….
 
按照上面的规律进行求解出第n个字符串是什么。
 
规律:相连的数字有多少个然后添加上这个数字
 
参考代码: 
 
package leetcode_50;

/***
*
* @author pengfei_zheng
* 按照规律进行求解字符串
*/
public class Solution38 {
public static String countAndSay(int n) {
if(n<=0) {
return "";
}
String s="1";
int times = 1;
while(times<n){
s = getSay(s);
times++;
}
return s;
}
private static String getSay(String s) {
int count =0;
StringBuilder str = new StringBuilder("");
for(int i = 0; i<s.length(); i++){
//first to add in order to prevent thinking about the index
count ++;
//not reach the end of s and next item is not equal to the pre item
if ((i< s.length()-1) && (s.charAt(i) != s.charAt(i + 1))) {
str = str.append(count).append(s.charAt(i));//rebuild the str
count = 0;//reset count to zero
}
else if ((i == s.length()-1)) {//meet the end of s
str = str.append(count).append(s.charAt(i));
}
}
return str.toString();
}
public static void main(String[]args){
String s = countAndSay(2);
System.out.println(s);
}
}

LeetCode 38 Count and Say(字符串规律输出)的更多相关文章

  1. LeetCode - 38. Count and Say

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

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

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

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

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

  4. leetcode 38 Count and Say ---java

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

  5. Java [leetcode 38]Count and Say

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

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

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

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

  8. LeetCode - 38. Count and Say(36ms)

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

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

随机推荐

  1. unity3d Matrix4x4列为主序

    unity3d的矩阵一直用,但是之前都是测试着用的,效果虽然正确,但是一直没搞清楚它是行矩阵还是列矩阵 今天测试了下 Matrix4x4 mat4 = Matrix4x4.Perspective(30 ...

  2. memcached监控工具

    最简单和最直接的方式是在启动memcached的时候加入-vv参数,从而在控制台打印每次客户端的请求和相应,这非常适合开发.另外一种较为直接的方式是通过telnet进行查看,例如:若server为本机 ...

  3. Umbraco中使用Related Links显示内部链接和外部链接

    在Umbraco的论坛里看到的办法,演示了如何在Umbraco中使用Related Links并显示的过程. 原文地址:http://www.nibble.be/?p=48

  4. fork 至 “sys_clone" SyS_clone

    注:glibc-2.17中fork的相应系统调用是sys_clone及SyS_clone.有人说调用的是sys_fork,但是我持否定意见,如果我们向真的来发起系统调用可以使用syscall. for ...

  5. 【CTR】各公司方法

    LR + 海量高纬离散特征 GBDT + 少量低纬连续特征 (Yahoo & Bing) GBDT + LR (FaceBook) FM + DNN (百度凤巢) MLR (阿里妈妈) FTR ...

  6. Lua基础语法讲解

    Lua 是什么? Lua 是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能. Lua 是巴西里约热内卢天主教大学( ...

  7. yum和apt-get用法及区别

    https://www.cnblogs.com/garinzhang/p/diff_between_yum_apt-get_in_linux.html

  8. 一个日志模板,从traceback打印异常受启发做的模板,可被pycharm esclip 等ide识别和跳转

    之前发过日志,再增加一种模板. logging.Formatter('%(asctime)s - %(name)s - File "%(pathname)s", line %(li ...

  9. mac版本cornerstone的无限期破解方法(转)

    CornerStone是个人非常喜欢的mac上的一款SVN客户端工具,官方提供了14天的免费试用(trail)版本.我们可以在此基础上提供无限期试用版本. 方法一:如果你从来没有安装过这个trail版 ...

  10. 正则 /\D/g

    onKeyUp="this.value=this.value.replace(/\D/g,'');"红色的是什么意识 /g是什么意思 ----------------------- ...