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 }
  • 相关阅读:
    bash 中有效建立锁
    go 语言 Makefile 指定依赖包位置
    在 mysql 中对特定的库禁用 DDL 语句
    go 语言并发机制 goroutine 初探
    Google和facebook如何应用R进行数据挖掘
    数据应用催生商业模式
    4款语音播报来电短信应用[Android]
    让 php 用 nginx 打包 zip
    10个关于 Dropbox 的另类功用(知乎问答精编)[还是转来了]
    分析以数据挖掘技术预测用户流失情况的方法
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4129937.html
Copyright © 2011-2022 走看看