zoukankan      html  css  js  c++  java
  • 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 public class Solution {
     2     public String countAndSay(int n) {
     3         // Note: The Solution object is instantiated only once and is reused by each test case.
     4         if(n < 1) return null;
     5         StringBuffer first = new StringBuffer();
     6         StringBuffer second = new StringBuffer();
     7         first.append(1);
     8         int a = 1;
     9         while(a < n){
    10             char per = first.charAt(0);
    11             int countf = 1;
    12             for(int i = 1; i < first.length(); i ++){
    13                 if(per == first.charAt(i)){
    14                     countf ++;
    15                 }else{
    16                     second.append(countf);
    17                     second.append(per);
    18                     per = first.charAt(i);
    19                     countf = 1;
    20                 }
    21             }
    22             second.append(countf);
    23             second.append(per);
    24             first = second;
    25             second = new StringBuffer();
    26             a ++;
    27         }
    28         return first.toString();
    29     }
    30 }

     第二遍:

     1 public class Solution {
     2     public String countAndSay(int n) {
     3         // Note: The Solution object is instantiated only once and is reused by each test case.
     4         if(n < 1) return null;
     5         String result = "1";
     6         for(int i = 0; i < n - 1; i ++){
     7             StringBuffer sb = new StringBuffer();
     8             int count = 1;
     9             char c = result.charAt(0);
    10             for(int j = 1; j < result.length(); j ++){
    11                 if(c != result.charAt(j)){
    12                     sb.append(count);
    13                     sb.append(c);
    14                     c = result.charAt(j);
    15                     count = 1;
    16                 }else{
    17                     count ++;
    18                 }
    19             }
    20             sb.append(count);
    21             sb.append(c);
    22             result = sb.toString();
    23         }
    24         return result;
    25     }
    26 }

     第三遍:

     1 public class Solution {
     2     public String countAndSay(int n) {
     3         String result = "1";
     4         if(n <= 1) return result;
     5         StringBuilder sb = new StringBuilder();
     6         while(n > 1){
     7             int count = 0;
     8             sb = new StringBuilder();
     9             for(int i = 0; i < result.length(); i ++){
    10                 if(i == 0 || result.charAt(i) == result.charAt(i - 1)) count ++;
    11                 else{
    12                     sb.append(count);
    13                     sb.append(result.charAt(i - 1));
    14                     count = 1;
    15                 }
    16             }
    17             sb.append(count);
    18             sb.append(result.charAt(result.length() - 1));
    19             result = sb.toString();
    20             n --;
    21         }
    22         return result;
    23     }
    24 }
  • 相关阅读:
    vim 编辑器使用
    PHP高并发高负载系统架构(转载)
    类的使用
    linux下EC20 4G模块驱动移植
    linux 4G模块拨号脚本
    linux4.1.4上移植ME909s-821,MU609 4G模块驱动
    shell脚本语之运算符
    vim的列编辑操作
    linux下普通用户添加 sudo 免密码
    4G模块在AM335x上的移植
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3365035.html
Copyright © 2011-2022 走看看