【leetcode刷题笔记】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.
题解:简单的模拟题,但是感觉题目叙述的不是特别清楚。
一开始给定一个“1”,把它说出来就是“one one”,写成串就是11,这样就得到了第二个串11;
把第二个串说出来就是“two one”,写成串就是21,这样就得到了第三个串21;
把第三个串说出来就是“one two one one”,写成串就是1211,这样就得到了第四个串1211;
......
题目求的是第n个串。
设置一个StringBuffer存放当前串“说出来的结果”,然后根据n的大小不断循环,最终得到第n个串即可。
代码如下:
public class Solution {
public String countAndSay(int n) {
String accumu = "1";
while(--n > 0){
StringBuffer sb = new StringBuffer();
char[] faster = accumu.toCharArray();
for(int i = 0;i < faster.length;){
int count = 1;
char now = faster[i];
i++;
while(i<faster.length && faster[i]== faster[i-1] ){
count++;
i++;
}
sb.append(String.valueOf(count)+now);
}
accumu = sb.toString();
}
return accumu;
}
}
【leetcode刷题笔记】Count and Say的更多相关文章
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- LeetCode刷题笔记(1-9)
LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...
- leetcode刷题笔记
(1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...
- leetcode刷题笔记08 字符串转整数 (atoi)
题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...
- 【leetcode刷题笔记】Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- LeetCode 刷题笔记 (树)
1. minimum-depth-of-binary-tree 题目描述 Given a binary tree, find its minimum depth.The minimum depth ...
随机推荐
- sessionStorage / localStorage
var referurl = document.referrer; //上级网址 if(referurl.indexOf('address_order')>0){ //判断是否是从上一级地址跳转 ...
- Jenkins与Docker相关的Plugin使用
原文地址:http://blog.csdn.net/ztsinghua/article/details/52128140 Jenkins与Docker相关的Plugin 在Jenkins Plugin ...
- Delphi 与 C/C++ 数据类型对照表(最新的tokyo)
更新,下面这table为最新的tokyo基本数据类型与C++的对照关系: Delphi to C++ types mapping Go Up to Support for Delphi Data ...
- JavaSE入门学习21:Java面向对象之接口(interface)(二)
一接口实现的多态 在上一篇博文:JavaSE入门学习20:Java面向对象之接口(interface)(一)中提到了接口的实现存在多态性,那么 这一篇主要就要分析接口实现的多态. 实例一 Test.j ...
- 一致性哈希算法PHP测试片段
<?php header('Content-type: text/html; charset=utf8');# 抽象接口interface hash{ public function _hash ...
- 在freemarker文件中,html标签获取后台的值
1.<#assign a='3333' /> 2.<input type="text" id="name" name="name&q ...
- ubuntu openfire Server install
1.首先登录到ubuntu server.在安装openfire 服务器之前,先确保你的系统已经更新到最新.然后输入下面的命令,一行一行执行,最后安装可用的更新 sudo apt-get update ...
- git学习之安装(二)
安装Git 最早Git是在Linux上开发的,很长一段时间内,Git也只能在Linux和Unix系统上跑.不过,慢慢地有人把它移植到了Windows上.现在,Git可以在Linux.Unix.Mac和 ...
- 【Mac + Pycharm】之实用东西以及配置东西
一.新建.py文件时默认模板: 步骤:File => Preferences for New Projects => Editor => File and Code Template ...
- jq bootstrap select 点击不能动弹
jq bootstrap select 点击不能动弹 因为是样式selectpicker 冲突. 解决办法换 样式 form-control <select name="ty ...