Java [leetcode 38]Count and Say
题目描述:
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
解题思路:
从第一个开始数起,对于输入n,则数n-1次即可。
代码如下:
public class Solution {
public String countAndSay(int n) {
int i = 1;
if (n <= 0)
return null;
String s = "1";
while (i <= n - 1) {
s = countAndSay(s);
i++;
}
return s;
}
public String countAndSay(String s) {
int count = 1;
StringBuffer sb = new StringBuffer();
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i - 1) == s.charAt(i))
count++;
else {
sb.append(count).append(s.charAt(i - 1));
count = 1;
}
}
sb.append(count).append(s.charAt(s.length() - 1));
return sb.toString();
}
}
Java [leetcode 38]Count and Say的更多相关文章
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- leetcode 38 Count and Say ---java
这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 --> 111221 --> 312211 --> ..... 1个 ...
- LeetCode 38 Count and Say(字符串规律输出)
题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111 ...
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- [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_Easy
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Java [Leetcode 357]Count Numbers with Unique Digits
题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...
- Leetcode 38 Count and Say 传说中的递推
class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...
随机推荐
- javac mac 终端乱码
java和javac在简体中文的Mac OSX的终端(Terminal.app)环境下,默认是以GBK编码的中文输出各种诸如语法错误,数组访问越界之类的信息.但是,Mac的终端的默认编码是UTF-8, ...
- 使用NPIO操作Excel
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NPOI.SS. ...
- sybase下convert函数第三个参数(时间格式)
convert(varchar(10),字段名,转换格式) 比如:1.select user_id,convert(varchar(10),dayts,11) as dates from tb_use ...
- IOS设备滑动事件
只要手指触摸屏幕,滑动,从屏幕离开,系统都会产生UIEvent对象类型的事件---当然包括UITouch事件 – touchesBegan:withEvent: 当用户触摸到屏幕时调用方法 – t ...
- 微软职位内部推荐-This Job is no longer available.
微软近期Open的职位: 如果你想试试这个职位,请跟我联系,我是微软的员工,可以做内部推荐.发你的中英文简历到我的邮箱:Nicholas.lu.mail(at)gmail.com
- python学习笔记1(语法)
语法 从"Hello,world"开始看吧,我们学的很多语言都是从helloworld开始的. >>> 1 + 1 2 >>> print 'H ...
- 1972: [Sdoi2010]猪国杀 - BZOJ
题目太长,我只发链接吧 wikioi(排版看起来舒服一点):http://www.wikioi.com/problem/1834/ bzoj:http://www.lydsy.com:808/Judg ...
- Android 环境搭建 版本问题
jdk1.6 1.7 eclipse 3.7.2 SDK-r12 ADT 12 SDK和ADT必须配套 搭建环境需要四个软件: 1.JDK(这是最新版本jdk1.7官方下载地址:http:// ...
- Unity3D调用第三方SDK(之一)从eclipse到Unity3D 友盟
原地址:http://www.360doc.com/content/14/0120/14/11670799_346638215.shtml 篇展示在Unity3D中调用友盟SDK的实现方法. 首先附上 ...
- hdu 2897 邂逅明下 博弈论
这个分区间: 1<=n%(p+q)<=p 必败 其他必胜! 代码如下: #include<iostream> #include<stdio.h> #include& ...