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 ...
随机推荐
- 【scrapy】使用方法概要(二)(转)
[请初学者作为参考,不建议高手看这个浪费时间] 上一篇文章里介绍了scrapy的主要优点及linux下的安装方式,此篇文章将简要介绍scrapy的爬取过程,本文大部分内容源于scrapy文档,翻译并加 ...
- MCP2515 : SPI CAN controller management
#ifndef __MCP2515_H #define __MCP2515_H /* mcp2515.h This file contains constants that are specific ...
- mt7620 wireless驱动特性意外发现
前言 今天又客户反映无线參数SSID编程了HT_AP0, 同事通过后台给他改动后反映给我,我想不正确啊,难道是无线驱动crash了?那应该不能玩才对啊... 追查线索 我们的路由器会定期汇报数据SSI ...
- .Net高级技术——程序集
程序集(Assembly),可以看做是一堆相关类打一个包,相当于java中的jar包(*).打包的目的:程序中只引用必须的程序集,减小程序的尺寸:一些程序集内部的类不想让其他程序集调用. 我们调用的类 ...
- Bootstrap 3之美01-下载并引入页面
本篇主要包括: ■ 下载Bootstrap 3■ Bootstrap 3引入页面 下载Bootstrap 3 →打开网站:http://getbootstrap.com/→点击屏幕中央位置的Down ...
- LR杂记-nmon+analyser监控linux系统资源
1.查看linux具体版本号信息 file /sbin/init 2.下载相应nmon版本号 http://pkgs.repoforge.org/nmon/ 3.安装 rpm -ivh nmon-14 ...
- MySQL Workbench update语句错误Error Code: 1175.
rom:http://www.tuicool.com/articles/NJ3QRz MySQL update error: Error Code: 1175. You are using safe ...
- Object类型转换为long或者Long
1.转换为long Object o = new Object();long l = Long.valueOf(String.valueOf(o)).longValue(); 2.转换为Long Ob ...
- Servlet监听器统计在线人数
监听器的作用是监听Web容器的有效事件,它由Servlet容器管理,利用Listener接口监听某个执行程序,并根据该程序的需求做出适应的响应. 例1 应用Servlet监听器统计在线人数. (1)创 ...
- C++ vector 删除符合条件的元素
C++ vector中实际删除元素使用的是容器vecrot中std::vector::erase()方法. C++ 中std::remove()并不删除元素,因为容器的size()没有变化,只是元素的 ...