【Problem】Count and Say
问题分析
题目理解:给定一个整数,如果相邻的几个数位数字是相同的,则输出重复出现的次数加上该数字本身;继续向后查找直到所有的数位处理完。
按照上述思路,则:
input |
output |
1 |
11 |
11 |
21 |
21 |
1211 |
但实际运行时提示出错,在输入为1的时候输出结果为11,而实际的应该是1。接着发现题目意思理解错了,如下图所示,后面的输出结果是依赖与前面的输出结果的。比如第一个输出为1;第二个输出是对1进行上述运算,即11;同理,第三个是对11进行运算,即21;接着依次是1211、111221、312211、13112221、1113213211……
源代码
public class Solution { public String countAndSay(int n) { if ( n == 1 ) { return "1"; } else { char current; // the current char int count; // the count of the current char String result = new String("1"); // the result int length = result.length(); // the length of the result string StringBuilder strBuilder = new StringBuilder(); // store the current result int i = 0; while ( n > 1 ) { for ( i = 0; i < length; i++ ) { current = result.charAt(i); count = 1; // while the next char is the same as the current char while ( (i+1 < length) && (result.charAt(i+1) == current) ) { count++; i++; } // add the char and its count to the current result strBuilder.append(count).append(current); } // update the result and its length, and clear the content of strBuilder for the next loop result = strBuilder.toString(); length = result.length(); strBuilder = new StringBuilder(); n--; } return result; } } }
【Problem】Count and Say的更多相关文章
- 【BZOJ2588】Count On a Tree(主席树)
[BZOJ2588]Count On a Tree(主席树) 题面 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第 ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
- 【BZOJ3956】Count 主席树+单调栈
[BZOJ3956]Count Description Input Output Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N ...
- 【优化】COUNT(1)、COUNT(*)、COUNT(常量)、COUNT(主键)、COUNT(ROWID)、COUNT(非空列)、COUNT(允许为空列)、COUNT(DISTINCT 列名)
[优化]COUNT(1).COUNT(*).COUNT(常量).COUNT(主键).COUNT(ROWID).COUNT(非空列).COUNT(允许为空列).COUNT(DISTINCT 列名) 1. ...
- 【hdu1705】Count the grid(皮克定理)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1705 [题意] 给出平面上三个点坐标,求围成的三角形内部的点数 做这道题需要先了解下皮克定理. 百度百科: ...
- 【mysql】 mybatis实现 主从表 left join 1:n 一对多 分页查询 主表从表都有查询条件 【mybatis】count 统计+JSON查询
mybatis实现 主从表 left join 1:n 一对多 分页查询 主表从表都有查询条件+count 需求: ======================================= ...
- 【BZOJ-3956】Count ST表 + 单调栈
3956: Count Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 173 Solved: 99[Submit][Status][Discuss] ...
- 【BZOJ-1833】count数字计数 数位DP
1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 2494 Solved: 1101[Submit][ ...
- 【leetcode】Count and Say (easy)
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
随机推荐
- 怎么去掉word标题前的黑点
原文地址:http://jingyan.baidu.com/article/9c69d48f593c5b13c9024e2c.html 我们在使用word时,标题前总是出现黑点,其实这个黑点在打印时是 ...
- uva156 By sixleaves
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <ma ...
- POJ 3368 RMQ-ST
一直感觉RMQ水,没自己写过,今天写了一道题,算是完全独立写的,这感觉好久没有了... 一直以来,都是为了亚洲赛学算法,出现了几个问题: 1.学的其实只是怎么用算法,对算法的正确性没有好好理解,或者说 ...
- 改变DM6467的内存划分
上次改过bbxm的http://blog.csdn.net/godofdsp/article/details/9377515,这次搞6467又遇到同样的问题了.按照bbxm的方法修改了内存划分,运行时 ...
- 学习本课程需要具备哪些基础及微信小程序目录结构介绍
1.html css js 基础 2.ajax 基础 3.简单的面向对象基础
- Windows下安装Python3.4.2
一.Windows下安装Python3.4.2 1.下载Windows下的Python3.4.2.exe 2.指定一个目录安装,然后下一步 3.配置环境变量包括Python.exe的文件.目录如下图所 ...
- Codeforces #245(div2)
A:A. Points and Segments (easy) 题目看了n久,開始认为尼玛这是div2的题目么,题目还标明了easy.. 意思是给你一n个点,m个区间,在n个点上放蓝球或者红球,然后让 ...
- DevExpress.GridControl.gridView的一些注意
1.DevExpress控件组中的GridControl控件不能使横向滚动条有效.现象:控件中的好多列都挤在一起,列宽都变的很小,根本无法正常浏览控件单元格中的内容. 解决: gridView1.Op ...
- javascript 【封装AJAX】
post function createXHR() { if (typeof XMLHttpRequest != 'undefined') { return new XMLHttpRequest(); ...
- asp.net mvc表单提交的几种方式
asp.net MVC中form提交和控制器接受form提交过来的数据 MVC中form提交和在控制器中怎样接受 1.cshtml页面form提交2.控制器处理表单提交数据4种方式方法1:使用传统的R ...