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< ;+ ...
随机推荐
- GHOST中DISK TO DISK 和DISK FROM to image的区别
Ghost的Disk菜单下的子菜单项可以实现硬盘到硬盘的直接对拷(Disk-To Disk)、硬盘到镜像文件(Disk-To Image)、从镜像文件还原硬盘内容(Disk-From Image)。 ...
- C语言标记化结构初始化语法
C语言标记化结构初始化语法 (designated initializer),而且还是一个ISO标准. #include <stdio.h> #include <stdlib.h&g ...
- java优化占用内存的方法(一)
java做的系统给人的印象是什么?占 内存!说道这句话就会有N多人站出来为java辩护,并举出一堆的性能测试报告来证明这一点.其实从理论上来讲java做的系统并不比其他语言开发出来的 系统更占用内存, ...
- touches获得手指点击的坐标
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObjec ...
- 微软职位内部推荐-Principal DEV Manager for Bing Client
微软近期Open的职位: Title: Principal DEV Manager for Bing ClientGroup: Search Technology Center Asia, BingW ...
- Xcode8 重新配置 CocoaPods -替换阿里源
一.CocoaPods是什么? CocoaPods是一个负责管理iOS项目中第三方开源库的工具.CocoaPods的项目源码在Github上管理.该项目开始于2011年8月12日,在这两年多的时间里, ...
- TWaver初学实战——如何在EasyUI中插入TWaver(续)
上次文章虽然简单易懂,但很有些小伙伴不满意:你这TWaver和EasyUI结合,只不过生硬地把TWaver图形插进去了,数据和人家EasyUI没一毛钱关系.嘿嘿,不就是想发生关系嘛,没问题啊!咱就还用 ...
- 浅谈JavaSccript函数与对象
函数 解剖函数 function One(leve1 , leve2){ //code return leve1+leve2 } 注释: 形参不需要加上类型: return语句为可选,没有return ...
- 2336: [HNOI2011]任务调度 - BZOJ
一道随机算法的题目 随便用什么随机算法 首先我们可以想到枚举类型3的最终类型,然后再做 先贪心出一个较优的序列,首先我们知道肯定是在A机器上先做完类型1的事件再做类型2的事件,机器B也类似,因为这些没 ...
- eclipse/MyEclipse 日期格式、注释日期格式、时区问题
eclipse/MyEclipse 日期格式.注释日期格式.时区问题 在eclipse/MyEclipse中,如果你的注释或是运行System.out.print(new java.util.Date ...