zoukankan      html  css  js  c++  java
  • [LeetCode#38]Count and Say

    Problem:

    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.

    Analysis:

    The problem is easy! You just need to construct a string from a given string iteratively. 
    But it include the skill in counting forward appearance!!!
    "111221", How to count it each in one pass?
    We know, only at "111(2)21", we know many '1' appears before it. The same for other characters!
    There is a special code structure for handling such problem.
    Key: We record the result of appearance when we reach the different character.
    -------------------------------------------------------------------------------
    key 1: the invariant require we know the count of previous charcter. We assigin count = 1, thus we could start from j = 1.
    
    int count = 1;
    for (int j = 1; j < res.length(); j++) {
        ....
    }
    -------------------------------------------------------------------------------
    key 2: Once the current character is the same as the prvious character, we increase the count.
    
    if (res.charAt(j) == res.charAt(j-1)) {
        count++;
    } 
    -------------------------------------------------------------------------------
    key 3: iff the current charcter is different from the privious one. We record the appearance of the previous character.
    
    if (res.charAt(j) != res.charAt(j-1)) {
        buffer.append(count);
        buffer.append(res.charAt(j-1));
        count = 1;
    }
    -------------------------------------------------------------------------------
    key 4: Since we delay the record of a character's appearance at next character, we must miss the last character's record, we must add it after the for loop.
    
    buffer.append(count);
    buffer.append(res.charAt(res.length()-1));
    res = buffer.toString();
    -------------------------------------------------------------------------------

    Solution:

    public class Solution {
        public String countAndSay(int n) {
            if (n <= 0)
                throw new IllegalArgumentException("n is not in the valid range");
            String res = "1";
            for (int i = 2; i <= n; i++) {
                StringBuffer buffer = new StringBuffer();
                int count = 1;
                for (int j = 1; j < res.length(); j++) {
                    if (res.charAt(j) == res.charAt(j-1)) {
                        count++;
                    } else {
                        buffer.append(count);
                        buffer.append(res.charAt(j-1));
                        count = 1;
                    }
                }
                buffer.append(count);
                buffer.append(res.charAt(res.length()-1));
                res = buffer.toString();
            }
            return res;
        }
    }
  • 相关阅读:
    第二篇 Entity Framework Plus 之 Query Future
    【转载】保哥 釐清 CLR、.NET、C#、Visual Studio、ASP.NET 各版本之間的關係
    第一篇 Entity Framework Plus 之 Audit
    搭建公司内部的NuGet Server
    第三篇:Entity Framework CodeFirst & Model 映射 续篇 EntityFramework Power Tools 工具使用
    封装的方法
    选项卡切换
    获取鼠标坐标并且根据鼠标位置不同弹出不同内容
    点击其它位置,div下拉菜单消失
    用js写的简单的下拉菜单
  • 原文地址:https://www.cnblogs.com/airwindow/p/4788531.html
Copyright © 2011-2022 走看看