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. xml根据属性去重。如csprj去重

    public static void distinct(string filePath) { //1.创建XML文档对象 XmlDocument doc = new XmlDocument(); // ...

  2. T4生成多文件时,不生成自己

    如:我用的网上的生成多文件的一个include文件. 生成多文件时,默认会生成一个以自己名字命名的文件如: 有一个demo.tt文件,生成时会出来一个demo.cs文件(默认情况下) 解决方法: Fo ...

  3. iOS: 查看 UIView 的视图树

    在想要查看的 UIView 附近打个断点,运行,直到停在断点处,在控制台键入:po [view recursiveDescription],回车. (lldb) po [self recursiveD ...

  4. drools研究后记

    在实际工作中,有关于达标推断的业务逻辑 就是谁谁谁 消费满了多少钱.就返多少钱的优惠券 声明:不是drools不好,仅仅是在我遇到的场景下,不合适,不够好 在使用drools的时候发现有例如以下问题: ...

  5. 给NSMutableArray添加copy属性就变成了NSArray

    -copy, as implemented by mutable Cocoa classes, always returns their immutable counterparts. Thus, w ...

  6. RMAN:简单的duplicate创建新数据库

    duplicate to "test" backup location '/home/oracle/11.2.0.4/assistants/dbca/templates/'; du ...

  7. linux下使用yum安装 mencached

    1. 安装 yum -y install memcached 2. 启动memcached ./usr/bin/memcached -d -m 256 -u root -p 11211 -c 1024 ...

  8. apache+tomcat集群部署笔记

    前提条件 安装gcc,gcc-c++两个编译器 yum install gcc yum install gcc-c++ 接下来开始安装集群相关环境: 1.下载apr,apr-util,pcre,apa ...

  9. Missing iOS Distribution signing identity

    打包上传appstore的时候报错如下: 解决方法: Download https://developer.apple.com/certificationauthority/AppleWWDRCA.c ...

  10. Spring-Mybatis --- 配置SqlSessionFactoryBean,整合Spring-Mybatis

    要利用Mybatis首先是需要导入mybatis-x.x.x.jar,其次,要整合Spring和Mybatis需要导入mybatis-spring-x.x.x.jar. JAR : mybatis-x ...