• 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);
        }
    }
  • 相关阅读:
    Tomcat报错:Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/JFreeChartTest]]
    Md5 加密,加盐值
    ajax 分页 步骤和代码
    【每天学习一点点】numpy中的reshape中参数为-1
    smali文件语法参考
    google zxing 二维码扫描(android client分析)
    cygwin编译ffmpeg移植到android平台问题集锦
    Sequoyah 本机开发Native Development: Invalid path for NDK(路径无效) 解决方案
    优化模式--数据局部性
    程序猿,你也配吃10元的盒饭?
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3821720.html
Copyright © 2020-2023  润新知