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);
        }
    }
  • 相关阅读:
    SQL Reporting Services Report Viewer Scroll Bar(RDLC)
    SQL Server日期格式转换大全
    Export GridView to Excel within an UpdatePanel
    ASP.NET CompareValidator validate Currency
    微軟 Office 2003 版本比較一覽表
    打开Safari的Javascript调试功能
    SQL SERVER 2000/2005 列转行 行转列
    [Java] 使用cookie保持Session (Axis2 和 WSIT)
    .NET通用权限系统快速开发框架
    Java进阶对象与内存控制(一)
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3821720.html
Copyright © 2011-2022 走看看