问题分析

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

  按照上述思路,则:

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. (二)boost库之字符串格式化

    (二)boost库之字符串格式化 程序中经常需要用到字符串格式化,就个人而言还是比较倾向于C格式的输出,如果只是打印日志,printf就够了,如果到生成字符串,获取你可以选择sprintf,但这些都是 ...

  2. LINUX下DNS的查看和配置

    linux下好像没有专门的DNS查看命令. 用ifconfig命令也是看不到DNS的信息.(也可能是我不知道) 本机的DNS配置信息是在:/etc/resolv.conf [root@localhos ...

  3. 深入理解事件(event)与委托(delegate)

    好久没学.NET了,最近重又开始学习,发现委托有很多变化了,发现事件不明白了(可能以前就没明白过) 网上搜索了几篇文章,也没注意什么时候的,发现都讲的不彻底,综合一下,也当个学习笔记. using S ...

  4. Embedded software develop step

    x86 –>embeded so you you  must familiar with x86 first-

  5. MongoDB 分片操作

    添加分片 use admin //添加分片节点,每个分片都是一个副本集[allowLocal:true仅仅开发时才将分片配置到本地,生产时不能这样] db.runCommand({addshard:& ...

  6. sharepoint 2010 使用自定义列表模版创建列表(1)

    我们在使用sharepoint的过程中,发现一个比较好用的功能,就是“将此列表另存为模版”(save list as template),这个功能的作用是,创建一个和当前列表或者文档库一模一样的配置, ...

  7. [C++ 2011 STL (VS2012 Update4) 源代码阅读系列(2)]熟悉一些宏定义和模版偏特化或叫模版专门化

    [C++ 2011 STL (VS2012 Update4) 源代码阅读系列(2)]熟悉一些宏定义和模版偏特化或叫模版专门化 // point_test.cpp : 知识点练习和测试,用于单步调试,跟 ...

  8. sql server中的 stuff 函数

    STUFF eg: SELECT stuff(12345,3,1,'-') result:    12-45 以下文段来自:http://www.cnblogs.com/lingxyd/archive ...

  9. 引用枚举进行对比时 enum需强制转换

    枚举类 public enum MailRead { /// <summary> /// 未读 /// </summary> UNREAD=0, /// <summary ...

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

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