zoukankan      html  css  js  c++  java
  • Leetcode-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.

    Analysis:

    The rule is:

    1 -> 11

    2 -> 12

    3 -> 13

    11 -> 21

    22 -> 22

    33 -> 23

    111 -> 31

    222 -> 32

    333 -> 33

    Solution:

     1 public class Solution {
     2     public String countAndSay(int n) {
     3         String curStr = "";
     4         if (n==0) return curStr;
     5         curStr="1";
     6         if (n==1) return curStr;
     7 
     8         for (int i=2;i<=n;i++){
     9             String newStr = "";
    10             int len = curStr.length();
    11             int index = 0;
    12             while (index<len){
    13                 char cur = curStr.charAt(index);
    14                 index++;
    15                 if (cur=='1') {
    16                     if (index+1<len && curStr.charAt(index)=='1' && curStr.charAt(index+1)=='1'){
    17                         newStr += "31";
    18                         index += 2;
    19                     } else if (index<len && curStr.charAt(index)=='1'){
    20                         newStr += "21";
    21                         index++;
    22                     } else newStr += "11";
    23                 } else if (cur=='2'){ 
    24                     if (index+1<len && curStr.charAt(index)=='2' && curStr.charAt(index+1)=='2'){
    25                         newStr += "32";
    26                         index += 2;
    27                     } else if (index<len && curStr.charAt(index)=='2'){
    28                         newStr += "22";
    29                         index++;
    30                     } else newStr += "12";
    31                 } else {
    32                     if (index+1<len && curStr.charAt(index)=='3' && curStr.charAt(index+1)=='3'){
    33                         newStr += "33";
    34                         index += 2;
    35                     } else if (index<len && curStr.charAt(index)=='3'){
    36                         newStr += "23";
    37                         index++;
    38                     } else newStr += "13";
    39                 }
    40             }
    41             curStr = newStr;
    42         }
    43 
    44         return curStr;       
    45     }
    46 }

     Solution 2:

    Solution 1 is a naive solution. Even though the number in the string cannot exceed 4, we should figure out a general solution.

     1 public class Solution {
     2     public String countAndSay(int n) {
     3         String res = "1";
     4         for (int i=2;i<=n;i++){            
     5             int index = 0;
     6             StringBuilder buf = new StringBuilder();
     7             while (index<res.length()){
     8                 int count = 1;
     9                 int index2 = index+1;
    10                 while (index2<res.length() && res.charAt(index2)==res.charAt(index)){
    11                     index2++;
    12                     count++;
    13                 }
    14 
    15                 buf.append((char)(count+'0'));
    16                 buf.append(res.charAt(index));
    17                 index = index2;
    18             }
    19             res = buf.toString();
    20         } 
    21 
    22         return res;
    23     }   
    24 }
  • 相关阅读:
    Linux下的lds链接脚本简介(一)
    linux字符驱动之poll机制按键驱动
    Linux中断处理驱动程序编写
    基于Hexo + Git + Nginx的博客发布
    Visual Studio2012打开时弹出“遇到异常:这可能是由某个扩展导致的”错误的解决办法
    卫星轨道和两行数据TLE
    C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)
    C# WinForm PropertyGrid用法
    C#基础系列:实现自己的ORM(反射以及Attribute在ORM中的应用)
    [转]计算机视觉、机器学习相关领域论文和源代码大集合
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4129937.html
Copyright © 2011-2022 走看看