问题分析

  题目理解:给定一个整数,如果相邻的几个数位数字是相同的,则输出重复出现的次数加上该数字本身;继续向后查找直到所有的数位处理完。

  按照上述思路,则:

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的更多相关文章

  1. 【BZOJ2588】Count On a Tree(主席树)

    [BZOJ2588]Count On a Tree(主席树) 题面 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第 ...

  2. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

  3. 【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 ...

  4. 【优化】COUNT(1)、COUNT(*)、COUNT(常量)、COUNT(主键)、COUNT(ROWID)、COUNT(非空列)、COUNT(允许为空列)、COUNT(DISTINCT 列名)

    [优化]COUNT(1).COUNT(*).COUNT(常量).COUNT(主键).COUNT(ROWID).COUNT(非空列).COUNT(允许为空列).COUNT(DISTINCT 列名) 1. ...

  5. 【hdu1705】Count the grid(皮克定理)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1705 [题意] 给出平面上三个点坐标,求围成的三角形内部的点数 做这道题需要先了解下皮克定理. 百度百科: ...

  6. 【mysql】 mybatis实现 主从表 left join 1:n 一对多 分页查询 主表从表都有查询条件 【mybatis】count 统计+JSON查询

    mybatis实现 主从表 left join  1:n 一对多 分页查询   主表从表都有查询条件+count 需求: ======================================= ...

  7. 【BZOJ-3956】Count ST表 + 单调栈

    3956: Count Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 173  Solved: 99[Submit][Status][Discuss] ...

  8. 【BZOJ-1833】count数字计数 数位DP

    1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 2494  Solved: 1101[Submit][ ...

  9. 【leetcode】Count and Say (easy)

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

随机推荐

  1. Signal ()函数详细介绍 Linux函数

    http://blog.csdn.net/ta893115871/article/details/7475095 Signal ()函数详细介绍 Linux函数 signal()函数理解 在<s ...

  2. javascript第十七课:this使用

    例如,我们要一个元素的值 function f1(){ alert(this.id); } document.getElementByid('#id').onclick=f1;  //将函数赋值给事件

  3. jQuery中设置form表单中action值的方法

    jQuery中设置form表单中action值的方法 (2011-03-17 10:18:19) 转载▼ 标签: 杂谈   html代码: <form id="myFormId&quo ...

  4. 关于OF和CF

    很久很久前写的.越来越意识到作为一名科班出身的学生的重要性. 自己在使用IDA时,发现F5产生类似的这种代码. 其中有一句,v5 <= -141920797,我在想为什么是负数.如果把-1419 ...

  5. 缓存 Array.length 是老生常谈的小优化

    问题 缓存 Array.length 是老生常谈的小优化. // 不缓存 for (var i = 0; i < arr.length; i++) { ... } // 缓存 var len = ...

  6. sticker.js贴纸效果

    http://stickerjs.cmiscm.com/ <div class="sticker gbtags"></div> <!-- 引用Java ...

  7. IBM WebSphere MQ的C#工具类以及源码(net)

    简单的介绍一下MQ常用的对象 Queue Manager 队列管理器 主要负责管理队列.通道等,类似与Oracle中的Oracle实例的概念,在一台服务器中可以定义多个Queue Manager. Q ...

  8. VirtualBox镜像复制载入

    转发:http://blog.csdn.net/dotuian/article/details/9127229 一,虚拟镜像文件格式 VirtualBox磁盘镜像文件(VDI, VMDK, VHD, ...

  9. 黑科技——编写一个无法卸载的App

    之前经常听到朋友或者新闻媒体上报道说,有的朋友的android手机中病毒了,出现了软件无法卸载的情况,对于我这样一个从事android开发程序员来说,我还不是太相信(毕竟自己还是有点菜,哈哈).今天在 ...

  10. 如何:控制命名空间前缀 (C#) (LINQ to XML)

    Visual Studio 2010 本主题介绍在序列化 XML 树时如何控制命名空间前缀. 在很多情况下,不需要控制命名空间前缀. 但是,某些 XML 编程工具需要命名空间前缀的特定控制. 例如,您 ...