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. ubuntu14.04安装好用的google拼音输入法

    装了ubuntu14.04后感觉自带的拼音输入法不好用的有没有,有些字拼不出来有没有...,其实我们安装google拼音输入发就会好很多... 方法/步骤     安装google拼音输入法 $sud ...

  2. 最简单的视音频播放演示样例3:Direct3D播放YUV,RGB(通过Surface)

    ===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...

  3. sqoop定时增量导入导出

    sqoop定时增量导入 2013-11-06 14:23 4553人阅读 评论(0) 收藏 举报 sqoop使用hsql来存储job信息,开启metastor service将job信息共享,所有no ...

  4. jquery获取元素颜色css('color')的值返回RGB

    css代码如下: a, a:link, a:visited { color:#4188FB; } a:active, a:focus, a:hover { color:#FFCC00; } js代码如 ...

  5. MySQL------报错Access denied for user 'root'@'localhost' (using password:NO)解决方法

    报错:Access denied for user 'root'@'localhost' (using password:NO) 原因:没有给用户“root'@'localhost”赋予数据库权限 解 ...

  6. Hibernate_day03讲义_使用Hibernate完成一对多的关系映射并操作

  7. 5 -- Hibernate的基本用法 --2 Hibernate入门

    5.2.1 Hibernate 下载和安装 5.2.2 Hibernate 的数据库操作 5.2.3 在Eclipse中使用Hibernate 啦啦啦

  8. SqlServer当前月份时间

    SqlServer当前月份时间 SELECT -DAY(getdate()+-DAY(getdate()))

  9. phpVirtualBox – 用浏览器操作虚拟机

    摘自:https://code.google.com phpVirtualBox 一个开源的,VirtualBox的用户界面,用PHP编写的AJAX实现.作为一个现代的Web界面,它允许你远程访问和控 ...

  10. Netty权威指南之伪异步I/O编程

    为了解决同步阻塞I/O一个链路需要一个线程处理问题,对BIO模型做了优化——后端通过一个线程池处理多个客户端的请求接入,设置线程最大值,防止线程并发接入导致的线程耗尽. 当有新的客户端接入时,将客户端 ...