算法5—字符串压缩和去除排序好数组里面重复的元素

来源:互联网 发布:恢复mac上icloud数据 编辑:程序博客网 时间:2024/05/21 06:42

问题:

把字符串压缩,比如aaabbbbc, 压缩后成为:a3b4c1。

分析:

这题很简单,我们只需要从头到尾遍历一遍字符串即可。首先设置一个计数器count, 每次“指针移位”的时候,判断当前字符是否与前一个字符相等,如果相等,count++, 指针继续下移,否则,我们需要对前面已经遍历的字符串进行处理,然后重新初始化count,直到字符串遍历结束。这题的关键是对最后一个字符的处理。

[java] view plain copy
  1. public static String compress(char[] array) {  
  2.         if (array == null || array.length == 0 ) return null;  
  3.           
  4.         int count = 1;  
  5.         StringBuilder sb = new StringBuilder();  //save the compressed string  
  6.           
  7.         for (int index = 1; index < array.length; index++) {  
  8.             if (array[index] == array[index - 1]) {  
  9.                 count++;  
  10.             } else {  
  11.                 sb.append(array[index - 1]);  
  12.                 sb.append(count);  
  13.                 count = 1;  
  14.             }  
  15.         }  
  16.         //important! add the last character to the stringbuilder.  
  17.         sb.append(array[array.length - 1]);  
  18.         sb.append(count);  
  19.           
  20.         return sb.toString();  
  21.                   
  22.     }  

—————————————————————————————————————————————————————————————————

问题:

对于一个已经排好序的数组,去除里面重复的元素,比如A = {1,2,2,2,3,3,4,4}, 去掉重复以后,就变成A = {1,2,3,4}.

原理非常简单,关键是写代码的时候注意“指针”位置,和如何比较重复,参见代码里的第一个for循环。

[java] view plain copy
  1. public int[] uniqueArray(int[] array) {  
  2.         if (array.length == 0return null;  
  3.         if (array.length == 1return array;  
  4.           
  5.         int pointer = 0;  
  6.           
  7.         for (int i = 1; i < array.length; i++) {  
  8.             if (array[i] != array[pointer]) {  
  9.                 pointer++;  
  10.                 array[pointer] = array[i];  
  11.             }   
  12.         }  
  13.           
  14.         //copy the data to another array  
  15.   
  16.         int[] uniArray = new int[pointer+1];  
  17.         for (int i = 0; i <= pointer; i++) {  
  18.             uniArray[i] = array[i];  
  19.         }  
  20.         return uniArray;  
  21.          
  22.     }  

技巧:利用set集合的特性去解决

[java] view plain copy
 print?
  1. import java.util.List;  
  2. import java.util.ArrayList;  
  3. import java.util.Set;  
  4. import java.util.HashSet;  
  5. public class lzwCode {  
  6.   
  7.     public static void main(String [] args) {  
  8.         testA();  
  9.         System.out.println("===========================");  
  10.         testB();  
  11.         System.out.println("===========================");  
  12.         testC();  
  13.     }  
  14.   
  15.     //去掉数组中重复的值  
  16.     public static void testA() {  
  17.         String [] str = {"Java""C++""Php""C#""Python""C++""Java"};  
  18.         for (String elementA:str ) {  
  19.             System.out.print(elementA + " ");  
  20.         }  
  21.         List<String> list = new ArrayList<String>();  
  22.         for (int i=0; i<str.length; i++) {  
  23.             if(!list.contains(str[i])) {  
  24.                 list.add(str[i]);  
  25.             }  
  26.         }  
  27.         /* 
  28.         Set<String> set = new HashSet<String>(); 
  29.         for (int i=0; i<str.length; i++) { 
  30.             set.add(str[i]); 
  31.         } 
  32.         String[] newStr =  set.toArray(new String[1]);  
  33.         */  
  34.         System.out.println();  
  35.         String[] newStr =  list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组   
  36.         for (String elementB:newStr ) {  
  37.             System.out.print(elementB + " ");  
  38.         }  
  39.         System.out.println();  
  40.     }  
  41.   
  42.     //删除数组中其中一个元素  
  43.     public static void testB() {  
  44.         String [] str = {"Java""C++""Php""C#""Python"};  
  45.         for (String elementA:str ) {  
  46.             System.out.print(elementA + " ");  
  47.         }  
  48.         //删除php  
  49.         List<String> list = new ArrayList<String>();  
  50.         for (int i=0; i<str.length; i++) {  
  51.             list.add(str[i]);  
  52.         }  
  53.         list.remove(2); //list.remove("Php")   
  54.         System.out.println();  
  55.         String[] newStr =  list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组   
  56.         for (String elementB:newStr ) {  
  57.             System.out.print(elementB + " ");  
  58.         }     
  59.         System.out.println();  
  60.     }  
  61.   
  62.     //在数组中增加一个元素  
  63.     public static void testC() {  
  64.         String [] str = {"Java""C++""Php""C#""Python"};  
  65.         for (String elementA:str ) {  
  66.             System.out.print(elementA + " ");  
  67.         }  
  68.         //增加ruby  
  69.         List<String> list = new ArrayList<String>();  
  70.         for (int i=0; i<str.length; i++) {  
  71.             list.add(str[i]);  
  72.         }  
  73.         list.add(2"ruby"); //list.add("ruby")   
  74.         System.out.println();  
  75.         String[] newStr =  list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组   
  76.         for (String elementB:newStr ) {  
  77.             System.out.print(elementB + " ");  
  78.         }     
  79.         System.out.println();  
  80.     }  
  81. }  

控制台结果:


——————————————————————————————————————————————————————————————————


0 0
原创粉丝点击