【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, ...
随机推荐
- Linux 上的数据可视化工具
Linux 上的数据可视化工具 5 种开放源码图形化工具简介 Linux® 上用来实现数据的图形可视化的应用程序有很多,从简单的 2-D 绘图到 3-D 制图,再到科学图形编程和图形模拟.幸运的是,这 ...
- poj1458 dp入门
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37551 Accepted: 15 ...
- poj 1887 Testing the CATCHER_最长上升子序列
题意:题目太长没看,直接看输入输出猜出是最长下降子序列 用了以前的代码直接a了,做法类似贪心,把最小的顺序数存在数组里面,每次二分更新数组得出最长上升子序列 #include<iostream& ...
- Linux开发环境配置
配置JDK: tar -xzvf jdk-7u71-linux-x64.tar.gz rm -f jdk-7u71-linux-x64.tar.gz 测试:java -version 配置Grad ...
- properties文件value换行处理方式
书写方式如下,就可以允许key1的value值换行了,但是整个过程要注意不要在文件中出现任何的非英文非半角的字符 key1=Where did you take the picture?\ ...
- cassandra命令
压力测试:cassandra-stress [command] -node [nodes] -mode thrift user=[user] password=[password] example: ...
- C# MySQL数据库的备份 还原 初始化
// 执行创建数据库操作 this.GetExecute(G_Con, "create database if not exists NEWDB"); this.sqlAddres ...
- asp.net中的绝对路径和相对路径
一.关于相对路径和绝对路径相对路径转绝对路径一般,我们在ASP.NET网站中往往需要把一个相对路径转化为绝对路径.通常是用Server.MapPath()方法.比如网站根目录下有 个"Upl ...
- Iframe和父窗口互调方法的集合
1.子iframe里调用父级的方法:window.parent.document. 2.父级里调用子集iframe:window.frames["iframe_text"].d ...
- css3 transiton
div { width:100px; height:100px; background:yellow; transition-property:width; transition-duration:1 ...