LeetCode 38 Count and Say(字符串规律输出)
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(字符串规律输出)的更多相关文章
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- [leetcode]38. Count and Say数数
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- leetcode 38 Count and Say ---java
这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 --> 111221 --> 312211 --> ..... 1个 ...
- Java [leetcode 38]Count and Say
题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...
- [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 ...
- Leetcode 38 Count and Say 传说中的递推
class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...
- 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 ...
- 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 ...
随机推荐
- JS jQuery json日期格式问题的办法
原生JS:Date对象详细参考 Date对象:基于1970年1月1日(世界标准时间)起的毫秒数 本文参考MDN做的详细整理,方便大家参考MDN 构造函数: new Date(); 依据系统设置的当前时 ...
- 用iostat对linux硬盘IO性能进行检测
近期公司安装了几台DELL PE2650和2850的服务器,统一安装的是RHLE5.132位系统,而服务器的SCSI硬盘都统一做了raid1.公司老总要求对硬盘IO作统一检测报告,在Linux下找了许 ...
- Ubuntu下PHP动态编译出现Cannot find autoconf的解决方法
执行phpize时出现Cannot find autoconf 错误 Ubuntu下解决方法 sudo apt-get install autoconf
- 对转换公式为LaTeX代码要注意什么
mathtype是一款专业的数学公式编辑工具,理科生专用的工具.mathtype公式编辑器能够帮助用户在各种文档中插入复杂的数学公式和符号.可以轻松的将数学公式转换成LaTex代码,但是转换LaTeX ...
- VS或编译的时候不生成Release文件夹
今天在编译第三方类的时候,总是发布的时候报没有第三方类库的的Release版本 解决方案: Build=>Configuration Manager=>Release 编译=>配置管 ...
- 错误 Unable to find vcvarsall.bat 的终极无敌最完美的解决办法
Windows 上通过 pip 安装 python 包,经常会出现这种错误. 如:pip install pyodbc. 这种错误的简单明了解释就是:python 编译器找不到计算机上面的 VC 编译 ...
- python __all__用法
主要是用来限定暴露的api a.py文件里面的内容 __all__ = ['major_fun'] def major_fun(): pass def assist_fun(): pass b.py ...
- [原] unity3d调用android版 人人sdk
开发过程 遇到天坑:纯android工程没问题,集成到unity3d中 就老提示 没登陆 .最后跟到底 发现是Util.java 中 openUrl 函数出的bug.unity3d 中调android ...
- Jersey 入门与Javabean
Jersey是JAX-RS(JSR311)开源参考实现用于构建RESTful Web service,它包含三个部分: 核心服务器(Core Server) 通过提供JSR 311中标准化的注释和AP ...
- Windows下切换盘符
方法: 直接输入盘符+引号,例如输入D:,不区分大小写. 使用cd命令,例如cd /d D: 使用cd命令有一些要注意的地方: 在同一个磁盘分区里,不需要加上\d,但是不同磁盘分区切换的时候,需要加上 ...