zoukankan      html  css  js  c++  java
  • Cracking the Coding Interview Q1.5

    Implement an method  to compress the string according to the number of occurrences of each char in the string. For example, "aabcccaa" is compressed as "a2b1c3a2". If the string after the compression is not shorter, return the original string.  

    package Question1_5;
    
    public class Question {
    
        public 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;
        }
        
        public static int countCompression(String str) {
            if (str == null || str.isEmpty()) return 0;
            char last = str.charAt(0);
            int size = 0;
            int count = 1;
            for (int i = 1; i < str.length(); i++) {
                if (str.charAt(i) == last) {
                    count++;
                } else {
                    last = str.charAt(i);
                    size += 1 + String.valueOf(count).length();
                    count = 1;
                } 
            }
            size += 1 + String.valueOf(count).length();
            return size;
        }
        
        public static String compressBad(String str) {
            int size = countCompression(str);
            if (size >= str.length()) {
                return str;
            }
            String mystr = "";
            char last = str.charAt(0);
            int count = 1;
            for (int i = 1; i < str.length(); i++) {
                if (str.charAt(i) == last) {
                    count++;
                } else {
                    mystr += last + "" + count;
                    last = str.charAt(i);
                    count = 1;
                }
            }
            return mystr + last + count;
        }
        
        public static String compressBetter(String str) {
            int size = countCompression(str);
            if (size >= str.length()) {
                return str;
            }
            StringBuffer mystr = new StringBuffer();
            char last = str.charAt(0);
            int count = 1;
            for (int i = 1; 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);
            return mystr.toString();
        }    
        
        public static String compressAlternate(String str) {
            int size = countCompression(str);
            if (size >= str.length()) {
                return str;
            }
            char[] array = new char[size];
            int index = 0;
            char last = str.charAt(0);
            int count = 1;
            for (int i = 1; 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);
            return String.valueOf(array);
        }
        
        public static void main(String[] args) {
            String str = "abbccccccde";
            int c = countCompression(str);
            String str2 = compressAlternate(str);
            String t = compressBetter("");
            System.out.println("Compression: " + t);
            System.out.println("Old String (len = " + str.length() + "): " + str);
            System.out.println("New String (len = " + str2.length() + "): " + str2);
            System.out.println("Potential Compression: " + c);
        }
    }
  • 相关阅读:
    再谈加密-RSA非对称加密的理解和使用
    WEB开发中的字符集和编码
    网页实时聊天之PHP实现websocket
    PHP中的回调函数和匿名函数
    shell实现SSH自动登陆
    初探PHP多进程
    PHP的openssl加密扩展使用小结
    搭建自己的PHP框架心得(三)
    docker 快速搭建Nexus3
    用图形数据库Neo4j 设计权限模块
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3821720.html
Copyright © 2011-2022 走看看