美文网首页
字符串压缩

字符串压缩

作者: feiyingmm | 来源:发表于2018-03-24 20:26 被阅读0次
package com.jack.test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test {
    public static String path = "aa.txt";
    
    public static void main(String[] args) {
        test1();
        test2();
    }
    
    /**
     * 从文本读取字符,压缩相邻相同字符
     * @throws Exception 
     */
    //@org.junit.Test
    public static void test1(){
        String str = readStrByStream().trim();
        Logger.getLogger(Test.class.getName()).log(Level.INFO,str);
        String str2 = readStrByReader();
        Logger.getLogger(Test.class.getName()).log(Level.INFO,str2);
        String[] arr = str.split("");
        int count = 1;
        for(int i=0;i<arr.length-1;i++){
            if(arr[i].equals(arr[i+1])){
                count++;
                if(i==arr.length-2)
                    System.out.print(count+arr[i]);
                continue;
            }
            System.out.print(count+arr[i]);
            count = 1;
            if(i==arr.length-2)
                System.out.print(count+arr[i+1]);
        }
        
        System.out.println();
    }
    
    

    /**
     * 顺序列出每个单词出现的次数
     */
    //@org.junit.Test
    public static void test2(){
        String str = "one two three one two three,one two three four";
        Logger.getLogger(Test.class.getName()).log(Level.INFO,str);
        String[] arr = str.split("[ ,]");
        
        String[] sortArr = new String[arr.length];//排序起作用
        Map<String,Integer> map = new HashMap<String,Integer>();//做数据对比,及计数
        for(int i=0;i<arr.length;i++){
            if(map.containsKey(arr[i])){
                map.put(arr[i], map.get(arr[i])+1);
            }else{
                map.put(arr[i], 1);
                sortArr[i] = arr[i];
            }
        }
        for(String s:sortArr){
            if(s==null) continue;
            System.out.print(map.get(s)+s+",");
        }
    }
    
    /*
     * 
     */
    public static void test3(){
        
    }
    
    
    public static void show(String[] arr){
        for(String s:arr)
            System.out.print(s+" ");
    }
    
    /**
     * 从文件中读取字符串
     * @return
     * @throws Exception 
     */
    @SuppressWarnings("resource")
    private static String readStrByStream(){
        StringBuilder build = new StringBuilder();
        InputStream in = null;
        try {
            //in = new FileInputStream(new File(getFile()));
            in = Test.class.getResourceAsStream(path);
            byte[] bytes = new byte[1024];
            while(in.read(bytes)!=-1){
                build.append(new String(bytes));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return build.toString();
    }
    
    private static String readStrByReader(){
        StringBuilder build = new StringBuilder();
        BufferedReader reader = null;
        try {
            //reader = new BufferedReader(new FileReader(new File(Test.class.getResource(path).getFile())));
            reader = new BufferedReader(new InputStreamReader(Test.class.getResourceAsStream(path)));
            String str = "";
            while((str = reader.readLine())!=null)
                build.append(str);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return build.toString();
    }
    
    private static String getFilePath(String path){
        //Test.class.getResourceAsStream(path).
        return null;
    }
    
    public static String getFile(){
        return Test.class.getResource(path).getFile();
    }
}

相关文章

  • 1394-字符串压缩

    字符串压缩 题目 字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabc...

  • LeetCode | 面试题 01.06. 字符串压缩【Pyth

    LeetCode 面试题 01.06. 字符串压缩【Easy】【Python】【双指针】 问题 力扣 字符串压缩。...

  • Java字符串压缩

    java 压缩字符串 如果源字符串长度小于64,压缩后的字符会比源字符串长。例如:str.length()=32c...

  • 2020-03-16 刷题1(字符串)

    01.06 字符串压缩 标签:字符串,内存题目其实很简单,用模拟法模拟字符串的压缩过程即可。但是我提交了三次,因为...

  • LeetCode 面试题 01.06. 字符串压缩

    题目 字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabcccccaa...

  • 面试题 01.06. 字符串压缩

    题目 字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabcccccaa...

  • 字符串压缩

    字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabcccccaaa会变...

  • 面试题 01.06. 字符串压缩

    题目:字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabcccccaa...

  • 面试题01.06_字符串压缩_hn

    题目描述 字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabccccc...

  • leetcode每日一题 python解法 3月16日

    难度:简单 题目内容: 字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串a...

网友评论

      本文标题:字符串压缩

      本文链接:https://www.haomeiwen.com/subject/phifcftx.html