zoukankan      html  css  js  c++  java
  • 统计字符 比如aaabbcca----3a2b1c1a

    package Demo;
    
    import java.util.Scanner;
    
    /**
     * Created by chengpeng on 16/11/3.
     */
    
    /*
    idea command+alt+o remove invalid import
     */
    public class StringStatistics {
    
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String str="";
            while(sc.hasNext()){
                str = sc.nextLine();//aaabbcca----3a2b1c1a
                System.out.println(statistics(str));
    
            }
            sc.close();
    
        }
    
    
        private static String statistics(String str) {
            if(str==null) return null;
            int begin=0;
            int end=str.length()-1;
            String reStr ="";
            int count =1;
            while(begin<=end){
                if ((begin<end-1)&&(str.charAt(begin)==str.charAt(begin+1))){
                    begin++;
                    count++;
                }else {
                    reStr+=count+String.valueOf(str.charAt(begin));
                    begin++;
                    count=1;
                }
    
            }
            return reStr;
        }
    }
  • 相关阅读:
    百斯特
    C++
    转载
    转载+整理
    转载
    转载
    转载
    C++
    转载
    CodeForces
  • 原文地址:https://www.cnblogs.com/chengpeng15/p/6027586.html
Copyright © 2011-2022 走看看