LeetCode 37 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
or
1"11
.
11
is read off as "two
or
1s"21
.
21
is read off as "one
, then
2one 1"
or 1211
.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
public class Solution {
public String countAndSay(int n) {
if (n == 0)
return "";
StringBuffer sb = new StringBuffer("1");
StringBuffer tempSB = new StringBuffer(); for (int i = 1; i < n; i++) {
int counter = 1;
char tempChar = sb.charAt(0);
for (int j = 1; j < sb.length(); j++) {
if (sb.charAt(j) == tempChar)
counter++;
else {
tempSB.append(""+counter + tempChar);
tempChar = sb.charAt(j);
counter = 1;
}
}
tempSB.append(""+counter + tempChar);
sb.delete(0, sb.length());
sb.append(tempSB);
tempSB.delete(0, tempSB.length());
}
return sb.toString();
}
}
或者
public String countAndSay(int n) {
if (n == 0) return "";
StringBuffer sb = new StringBuffer("1");
StringBuffer tempSB = new StringBuffer(); for (int i = 1; i < n; i++) {
int counter = 1;
char tempChar = sb.charAt(0);
for (int j = 1; j < sb.length(); j++) {
if (sb.charAt(j) == tempChar)
counter++;
else {
tempSB.append(Integer.toString(counter)+ tempChar);
tempChar = sb.charAt(j);
counter = 1;
}
}
tempSB.append(Integer.toString(counter)+ tempChar);
sb.delete(0, sb.length());
sb.append(tempSB);
tempSB.delete(0, tempSB.length());
}
return sb.toString();
}
LeetCode 37 Count and Say的更多相关文章
- leetcode 36 有效的数独 哈希表 unordered_set unordersd_map 保存状态 leetcode 37 解数独
leetcode 36 感觉就是遍历. 保存好状态,就是各行各列还有各分区divide的情况 用数组做. 空间小时间大 class Solution { public: bool isValidSud ...
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- leetcode@ [327] Count of Range Sum (Binary Search)
https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- leetcode 730 Count Different Palindromic Subsequences
题目链接: https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 730.Count ...
- LeetCode 38 Count and Say(字符串规律输出)
题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111 ...
随机推荐
- Android Tasker应用之自动查询并显示话费流量套餐信息
Android Tasker应用之自动查询并显示话费流量套餐信息 虽然Android平台有非常多的流量监控软件,但最准确的流量数据还是掌握在运营商手里.有些朋友可能像我一样时不时地发短信查询流量信息, ...
- Java基础加强总结(二)——泛型
一.体验泛型 JDK1.5之前的集合类中存在的问题——可以往集合中加入任意类型的对象,例如下面代码: package cn.gacl.generic.summary; import java.util ...
- java转换emoji表情
/** * @Description 将字符串中的emoji表情转换成可以在utf-8字符集数据库中保存的格式(表情占4个字节,需要utf8mb4字符集) * @param str * 待转换字符串 ...
- Java Web开发基础(2)-JSP
上一篇博我粗略的介绍了一下Servlet.粗略是由于博主也刚刚学习这部分的内容,还不是非常懂所以无法讲的非常精细.可是本着二八原则,我还是先继续学习.所以,这篇博客接着JSP的内容.由于.这两个内容关 ...
- 第二章 Java浮点数精确计算
1.实际意义 在实际开发中,如果需要进行float或double的精确计算(尤其是财务计算),直接使用float或double是不行的(具体的例子看下边的代码的main方法的测试结果),需要使用Big ...
- asm rebalance 原理
详见原文博客链接地址: asm rebalance 原理
- go语言基础之结构体成员的使用指针变量
1.结构体成员的使用:指针变量 示例: package main //必须有个main包 import "fmt" //定义一个结构体类型 type Student struct ...
- Android动画-补间(Tween)动画
Android动画的两种方式,其中帧动画上篇文章已经讲了,这次主要讲解的就是补间动画,补间动画就是动画业务场景中常用的旋转,平移,缩放,和渐变效果,帧动画是通过轮播动画实现动画效果,补间动画通过在两个 ...
- retrofit okhttp RxJava bk Gson Lambda 综合示例【配置】
项目地址:https://github.com/baiqiantao/retrofit2_okhttp3_RxJava_butterknife.git <uses-permission andr ...
- JS操作JSON常用方法
一.JSON字符串的替换 工作经常遇到这样的字符串,如下: 需要经过替换后,才能从字符串转化成JSON对象.这里我们需要用JS实现replaceAll的功能, 将所有的 ' \\" ' 替换 ...