问题分析

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

  按照上述思路,则:

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. Noip2013之路

    当我回望过去的一年,我想,我对自己没有任何的愧疚,因为我每一个脚印,都踩的很坚实. 第一次参加模拟赛,第一次接触NOIP的规则,虽然考得不是特别好,但是还是很有收获的,首先,数组一定要开得足够大,不然 ...

  2. [Leetcode][Python]49: Anagrams

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 49: Anagramshttps://leetcode.com/proble ...

  3. linux监控脚本

    1,snmp安装脚本for ubuntu/CentOS #!/usr/bin/env bash export LC_ALL=C " ] then >& exit fi #### ...

  4. python模块目录文件后续

    1,新增PythonModule加载path Ruiy tip(关于python list[]数据库类型特殊你懂的!append(""),extend([""] ...

  5. javascript第十八课:windowd对象的方法

    window.confirm('确定删除吗?'); //当用户点击一个按钮出发一个事件的时候,弹出一个提示框让网友确认,返回一个bool值,点确定的话,返回true,点取消,返回flase windo ...

  6. 最少步数(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 ...

  7. android listiew适配器

    List<Map<String>> Items = new ArrayList<Map<String>>(); // 把该显示的内容放到list中 fo ...

  8. SEO,你敢说你会吗?

    SEO从何開始学起?SEO基础知识我们须要掌握哪些?SEO是什么?SEO究竟要怎么做?这些问题无时无刻不在困惑着我们.就我个人一些小观点来谈谈一个站点优化的大致步骤. SEO初期阶段 空间域名:空间跟 ...

  9. 实用CSS3属性之 :target伪类实现Tab切换效果

    CSS3 :target伪类用来改变页面中锚链接URL所指向的ID样式,例如你要改变描链接指向#tab的元素字体颜色为蓝色,哪么你可以这样写成#tab:target {color:blue} 浏览器支 ...

  10. JqueryUI-3

    本文在于巩固基础 JqueryUI控件之滚动条 这次主要是看Jquery官网上的API文档来做的 <!DOCTYPE html> <html> <head lang=&q ...