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. VC++ 内存泄露与检测的一种方法

        本文介绍,当VC++或者MFC程序,出现内存泄露时,如何快速定位的方法,这种方法有一定的局限性,在注意事项中会给出的. MFC程序     当MFC程序出现内存泄露时,退出程序时的VS调试输出 ...

  2. Spring MVC异常处理详解 ExceptionHandler good

    @ControllerAdvice(basePackageClasses = AcmeController.class) public class AcmeControllerAdvice exten ...

  3. zabbix监控系列(5)之通过trap模式监控网络设备

  4. Android 代码自动提示功能

    Eclipse for android 实现代码自动提示智能提示功能,介绍 Eclipse for android 编辑器中实现两种主要文件 java 与 xml 代码自动提示功能,解决 eclips ...

  5. Bootstrap——网站添加字体图标

    @font-face { font-family: 'itcast'; src: url('../font/MiFie-Web-Font.eot') format('embedded-opentype ...

  6. python __all__用法

    主要是用来限定暴露的api a.py文件里面的内容 __all__ = ['major_fun'] def major_fun(): pass def assist_fun(): pass b.py ...

  7. C# 多线程 Parallel.ForEach 和 ForEach 效率问题研究及理解

    from:https://blog.csdn.net/li315171406/article/details/78450534 最近要做一个大数据dataTable循环操作,开始发现 运用foreac ...

  8. Eclipse------如何将项目通过maven编译并打包

    1.右击项目>>>点击Debug As>>>点击 Maven install进行编译,编译成功后入图 2.右击项目>>>点击Debug As> ...

  9. MySQL --- 计算指定日期为当月的第几周

    SET @d=NOW(); ; 啦啦啦

  10. 【遥感影像】Python GDAL 像素与坐标对应

    转:https://blog.csdn.net/theonegis/article/details/50805520 https://blog.csdn.net/wsp_1138886114/arti ...