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);
        }
    }
  • 相关阅读:
    day23
    day22
    day21
    day20
    小程序 组件操作
    jmeter安装使用一
    小程序登录操作
    Django ORM DateTimeField 时间误差8小时问题
    小程序初始篇
    ADB命令
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3821720.html
Copyright © 2011-2022 走看看