`

Count and Say

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

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

一道很有意思的题目,第一个状态为1,第二个状态为11(一个1),第三个状态为21(两个1),第四个状态为1211(一个2和一个1),第五个状态为111221(一个1,一个2,两个1),. . .,让我们找到第n个状态。我们可以从第一个状态开始处理,一直衍生到第n个状态。当然状态衍生下一代时,我们从第一个数子开始比较,用一个计数counter来记录相同数字的个数,遇到不同的就讲当然counter的值和当然数字加入到字符串中,同时将counter置1,这样一直遍历完当前的状态,从而得到下一个状态。代码如下:
public class Solution {
    public String countAndSay(int n) {
        if(n <= 0) return "";
        String result = "1";
        int i = 1;
        while(i < n) {
            StringBuilder sb = new StringBuilder();
            int count = 1;
            for(int j = 1; j < result.length(); j++) {
                if(result.charAt(j) == result.charAt(j - 1)) {
                    count ++;
                } else {
                    sb.append(count);
                    sb.append(result.charAt(j - 1));
                    count = 1;
                }
            }
            sb.append(count);
            sb.append(result.charAt(result.length() - 1));
            result = sb.toString();
            i ++;
        }
        return result;
    }
}
分享到:
评论

相关推荐

    LeetCode原题Count and Say

    Leetcode原题Count and Say count-and-say序列是整数序列,前五个术语如下: 1. 1 2. 11 3. 21 1211 5. 111221 1被读作“1”或11。 11被读作“两个1”或21。 21被读作“一个2,然后一个1”或1211。 给定整数n,...

    cpp-算法精粹

    Count and Say Anagrams Valid Anagram Simplify Path Length of Last Word Isomorphic Strings Word Pattern 栈和队列 栈 Min Stack Valid Parentheses Longest Valid Parentheses Largest Rectangle in Histogram ...

    LeetCode解题总结

    3.12 Count and Say 3.13 变位词 3.14 简化系统路径 3.15 最后一个单词的长度 3.16 反转字符串中的单词 3.16.1 字符串前后和中间可能存在多个空格 3.16.2 不存在前后和中间的多余空格 3.17 一个编辑距离 4. 栈 4.1 ...

    LeetCode最全代码

    I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "馃摉" means you need to subscribe to [LeetCode premium membership](https://leetcode.com/subscribe/) for the ...

    leetcode中国-leetcode:leetcode刷题

    leetcode中国 我自己的leetcode刷题记录 ###[20150920] ...count and say , easy , 模拟 Anagrams 字符串处理,map Simplify Path,字符串处理,stack Length of Last Word,字符串处理.细节题 Rever

    leetcode答案-LeetCode:Swift中的LeetCode

    leetcode 答案LeetCode LeetCode in Swift 这个Repo 用来存下我在LeetCode ...Count and Say Easy #53 Maximum Subarray Easy #66 Plus One Easy #70 Climbing Stairs Easy #83 Remove Duplicates from Sorted L

    lovely-nuts:这是一个可爱的坚果

    Practice-Leetcode 这是一个Chinese School Girl:China:用来练习leetcode的文档.每道下面的题都有详细的解题思路,和知识点分析,尽请参考。 找实习的小伙伴也可以参考我的Tech-blog...038.Count and Say 递归 040.C

    leetcode卡-LeetCode:LeetCode题解

    leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 备注 0008 String to Integer(atoi) ...Count and Say :star: 0043 Multiply Strings :star: :star: 大数相乘 0044 Wild Card Matchi

    leetcode给单链表加一js实现-leetcode-By[removed]LeetCode解决方案(全部通过JavaScript!)h

    leetcode给单链表加一js实现 LeetCode By JavaScript LeetCode Solutions (All By JavaScript!) 的个人Solutions汇总,全部使用JavaScript完成:grinning_squinting_face: 目的:了解、掌握数据结构和算法,当然...Say:

    Look And Say 序列php实现代码

    ………… 根据详细的说明可以参见:http://en.wikipedia.org/wiki/Look-and-say_sequence 下面用PHP实现这个序列,如下: 复制代码 代码如下: function look($str) { $len = strlen($str); $count=0; $result=”

    say20-React:使用 react 构建 say20(计数游戏)的简单 Web 界面

    One may count upto one or two numbers per turn, and the numbers must be in counting order. One must start with the number after the last one that the other one said. For example, the first person ...

    微软内部资料-SQL性能优化3

    Lesson 1: Concepts – Locks and Lock Manager 3 Lesson 2: Concepts – Batch and Transaction 31 Lesson 3: Concepts – Locks and Applications 51 Lesson 4: Information Collection and Analysis 63 Lesson 5:...

    光线追踪的程序实现raytracing

    And they prove it: Imagine a huge scene, consisting of, say, 50 million triangles. Toss it at a recent GeForce with enough memory to store all those triangles, and write down the frame rate. It will ...

    Expert Oracle Database Architecture 2nd Edition

    count was for 9i, and that was more than 20,000 pages—and I doubt if the number has dropped in 10g and 11g. With three (fairly slim) manuals for 5.0.22, it didn’t take me very long to learn about ...

    微软内部资料-SQL性能优化2

    Troubleshooting server performance-based support calls requires product knowledge, good communication skills, and a proven troubleshooting methodology. In this module we will discuss Microsoft® SQL ...

    kgb档案压缩console版+源码

    PAQ6v2 - File archiver and compressor. (C) 2004, Matt Mahoney, mmahoney@cs.fit.edu This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public ...

    VB编程资源大全(英文源码 其它)

    I can't say much more about it except for the fact that its fast and compact&lt;END&gt;&lt;br&gt;51,chexer.zip As Jonathan wrote, "This is a tool I wrote to use in DevStudio to facilitate memory address ...

    leetcode和oj-OJ_Solution:leetcode、poj等OJ解决方案

    https://leetcode.com/problems/count-and-say/ 3, java/dungenegame https://leetcode.com/problems/dungeon-game/ 4, java/findminrotaed 5, java/houserobber https://leetcode.com/problems/house-robber/

    2008年6月大学英语六级A卷真题

    Sydney Brenner, senior distinguished fellow of the Crick-Jacobs Center in California, won the 2002 Nobel Prize for Medicine and says that if there is a global disaster some humans will survive-and ...

    微软内部资料-SQL性能优化5

    Another way to say this is that the data itself is part of the clustered index. A clustered index keeps the data in a table ordered around the key. The data pages in the table are kept in a doubly ...

Global site tag (gtag.js) - Google Analytics