问题分析

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

  按照上述思路,则:

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. 带有OUTPUT的INSERT,DELETE,UPDATE

    原文地址:http://blog.sina.com.cn/s/blog_71460d950100nld2.html OUTPUT是SQL SERVER2005的新特性.可以从数据修改语句中返回输出.可 ...

  2. 最少步数(dfs + bfs +bfs优化)

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  3. 【单调队列】【3-21个人赛】【problmeB】

    Problem B Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Sub ...

  4. Array数组方法的总结

    添加元素: 1. push(arg1,arg2,arg3....)  向数组的尾部添加元素,返回值是数组的长度. 2.unshift(arg1,arg2,arg3....) 向数组的头部添加元素,返回 ...

  5. Jquery揭秘系列:谈谈bind,one,live,delegate,on事件及实现

    在Jquery里面,我们用的最多的就是事件绑定了,事件绑定有多个函数.例如:bind,one,live,delegate,on等等. on() jQuery事件绑定.on()简要概述及应用 看源码发现 ...

  6. UVA 1603 Square Destroyer

    题意: 给定一个火柴棒拼成的方格阵,然后去掉一些火柴棒,问至少再去掉几根火柴棒能够让图中一个正方形都没有. 思路: 1. 由于题目中给定了 n 的范围,2 * n * (n + 1) <= 60 ...

  7. RDLC报表系列(二) 行分组

    接上一篇文章的内容,今天来说的是行分组.还是打开demo1.rdlc界面,拖入一个文本框和表 1.在表中随便选择一个字段,不然在添加行组的时候不会自动提示.我这里是选择的Dept 2.在下面的行组中右 ...

  8. C#根据函数名称执行对应的函数

    using System; using System.Collections.Generic; using System.Reflection; namespace test { public cla ...

  9. Android中view的事件

    view:top.left.right.bottom,相对于parent的位置参数,获取通过get*()来获取.width=right-left.height=bottom-top.x=left+tr ...

  10. zoj1136 Multiple

    记忆化搜索,因为要求最小的,肯定是从小到大,依次添加,那么通过bfs,队列貌似是最好的选择.因为很可能那个数爆long long,所以采用字符串存储,并记录余数,通过模拟除法的方式来写. 剪枝:因为后 ...