9.1数组与字符串(四)——字符串压缩

/**
 * 功能:利用字符重复出现的次数,实现基本的字符串压缩功能。比如,字符串 aabcccccaaa会变成
 * a2b1c5a3.若压缩后的字符串没有变短,则返回原来的字符串。
 */
五种方法:
1、用两个数组分别存放字符和对应的出现次数
     //Mine:用两个数组分别存放字符和出现次数
       public static void compressBad(String str ){
             char [] s = new char [ str .length()];
             int [] num = new int [ str .length()];
             int k =0;
             s [ k ]= str .charAt(0);
             num [ k ]=1;
             for ( int i =1; i < str .length(); i ++){
                   if ( s [ k ]== str .charAt( i )){
                         num [ k ]++;
                  } else {
                         s [++ k ]= str .charAt( i );
                         num [ k ]=1;
                  }     
            }
            
             int i =0;
             while ( num [ i ]!=0){
                  System. out .print( s [ i ]);
                  System. out .print( num [ i ]);
                   i ++;
            }
            System. out .println();
      }
 
2、用HashMap存放字符和出现的频率,但不是用于本题目 (可参考遍历HashMap)   
       //HashMap不适用本题,适合求字符数的总数
       public static void compressBad2(String str ){
            HashMap<Character,Integer> map = new HashMap<Character, Integer>();
             for ( int i =0; i < str .length(); i ++){
                   if ( map .containsKey( str .charAt( i ))){
                         map .put( str .charAt( i ), map .get( str .charAt( i ))+1);
                  } else {
                         map .put( str .charAt( i ), 1);
                  }
            }
            Set<Entry<Character,Integer>> sets = map .entrySet();
             for (Entry<Character,Integer> entry : sets ){
                  System. out .print( entry .getKey());
                  System. out .print( entry .getValue());
            }
            System. out .println();
      }
 
3、迭代访问字符串,并将字符串拷贝到新字符串,输出重复字符。    
       //迭代访问字符串,将字符拷贝到新字符串,并输出重复字符。
       public static void compressBad3(String str ){
            String myStr = "" ;
             char last = str .charAt(0);
             int count =0;
             for ( int i =0; i < str .length(); i ++){
                   if ( last == str .charAt( i )){
                         count ++;
                  } else {
                         myStr += last + "" + count ;
                         last = str .charAt( i );
                         count =1;
                  }
            }
             myStr += last + "" + count ;
            System. out .println( myStr );
      }
4、 加入StringBuffer加快字符拼接操作,同时加入了长度检查      
       //加入StringBuffer加快字符拼接操作,同时加入了长度检查。
       public static void compressBad4(String str ){
             int size = countCompression( str );
             if ( size >= str .length())
                  System. out .println( str );
            
            StringBuffer myStr = new StringBuffer();
             char last = str .charAt(0);
             int count =0;
             for ( int i =0; i < str .length(); i ++){
                   if ( str .charAt( i )== last ){
                         count ++;
                  } else {
                         myStr .append( last );
                         myStr .append( count );
                         last = str .charAt( i );
                         count =1;
                  }
            }
             myStr .append( last );
             myStr .append( count );
            System. out .println( myStr );
      }     
       static int countCompression(String str ){
             if ( str == null || str .isEmpty())
                   return 0;
             char last = str .charAt(0);
             int size =0;
             int count =0;
             for ( int i =0; i < str .length(); i ++){
                   if ( str .charAt( i )== last ){
                         count ++;
                  } else {
                         size +=1+String. valueOf( count ).length();
                         last = str .charAt( i );
                         count =1;
                  }
            }
             size +=1+String. valueOf( count ).length();
            
             return size ;
      }
      
5、不使用StringBuffer,采用字符数组存放。
       public static void compressBad5(String str ){
             int size = countCompression2( str );
             if ( size >= str .length())
                  System. out .println( str );
            
             char [] array = new char [ size ];
             char last = str .charAt(0);
             int count =0;
             int index =0;
             for ( int i =0; i < str .length(); i ++){
                   if ( str .charAt( i )== last ){
                         count ++;
                  } else {
                         index = setChar( array , last , index , count );
                         last = str .charAt( i );
                         count =1;
                  }
            }
             index = setChar( array , last , index , count );
            System. out .println(String. valueOf( array )); //直接打印字符数组
      }
       static int setChar( char [] array , char c , int index , int count ){
             array [ index ]= c ;
             index ++;
            
             char [] cnt =String. valueOf( count ).toCharArray();
             for ( char x : cnt ){
                   array [ index ]= x ;
                   index ++;
            }
            
             return index ;
      }
       static int countCompression2(String str ){
             if ( str .isEmpty()|| str == null )
                   return 0;
             int size =0;
             char last = str .charAt(0);
             int count =0;
             for ( int i =0; i < str .length(); i ++){
                   if ( str .charAt( i )== last ){
                         count ++;
                  } else {
                         size +=1+String. valueOf( count ).length();
                         last = str .charAt( i );
                         count =1;
                  }
            }
             size +=1+String. valueOf( count ).length();
            
             return size ;
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值