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      
     4        String say = "1";
     5        for(int i = 1; i < n ; i++){
     6            say = build(say);
     7        }
     8        return say;
     9     }
    10     
    11     public String build(String s){
    12         int len = s.length();
    13         int last = 0;
    14         String temp = "";
    15         for(int i = 0; i < len; i++){
    16             if(s.charAt(last) != s.charAt(i)){
    17                 temp = temp + (i-last)+s.charAt(last);
    18                 last = i;
    19             }
    20         }
    21         // 最后i走到length的时候 还是和last index字符一样,所以要把这个加上
    22         if(last < len){
    23             temp = temp + (len -last) + s.charAt(last);
    24         }
    25         
    26         return temp;
    27     }
    28 }
  • 相关阅读:
    多表代换密码
    仿射变换
    LeetCode实战练习题目
    13.线性同余方程 扩展欧几里得算法
    12.扩展欧几里得算法
    11.快速幂求逆元
    10.快速幂
    9.筛法求欧拉函数
    8.欧拉函数
    7.最大公约数 欧几里得算法,也叫辗转相除法
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3545735.html
Copyright © 2011-2022 走看看