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);
        }
    }
  • 相关阅读:
    php中字符与字节的区别
    sql把两值之和当作条件进行查询
    Intel® Neural Compute Stick 2
    使用OpenCV的VideoCapture 读取.mp4文件时出现以下错误:Unable to stop the stream: Inappropriate ioctl for device
    更换Raspbian Buster源
    OpenCV之cv2函数 2
    OpenCV中cv2的用法
    OpenVINO 对象识别 real_time_object_detection Movidius
    树莓派和计算棒实现图形识别 RaspBerry Pi4 with OpenVino and Movidius
    树莓派无线网卡老是掉线
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3821720.html
Copyright © 2011-2022 走看看