问题:
把字符串压缩,比如aaabbbbc, 压缩后成为:a3b4c1。
分析:
这题很简单,我们只需要从头到尾遍历一遍字符串即可。首先设置一个计数器count, 每次“指针移位”的时候,判断当前字符是否与前一个字符相等,如果相等,count++, 指针继续下移,否则,我们需要对前面已经遍历的字符串进行处理,然后重新初始化count,直到字符串遍历结束。这题的关键是对最后一个字符的处理。
- public static String compress(char[] array) {
- if (array == null || array.length == 0 ) return null;
- int count = 1;
- StringBuilder sb = new StringBuilder(); //save the compressed string
- for (int index = 1; index < array.length; index++) {
- if (array[index] == array[index - 1]) {
- count++;
- } else {
- sb.append(array[index - 1]);
- sb.append(count);
- count = 1;
- }
- }
- //important! add the last character to the stringbuilder.
- sb.append(array[array.length - 1]);
- sb.append(count);
- return sb.toString();
- }
—————————————————————————————————————————————————————————————————
问题:
对于一个已经排好序的数组,去除里面重复的元素,比如A = {1,2,2,2,3,3,4,4}, 去掉重复以后,就变成A = {1,2,3,4}.
原理非常简单,关键是写代码的时候注意“指针”位置,和如何比较重复,参见代码里的第一个for循环。
- public int[] uniqueArray(int[] array) {
- if (array.length == 0) return null;
- if (array.length == 1) return array;
- int pointer = 0;
- for (int i = 1; i < array.length; i++) {
- if (array[i] != array[pointer]) {
- pointer++;
- array[pointer] = array[i];
- }
- }
- //copy the data to another array
- int[] uniArray = new int[pointer+1];
- for (int i = 0; i <= pointer; i++) {
- uniArray[i] = array[i];
- }
- return uniArray;
- }
技巧:利用set集合的特性去解决
- import java.util.List;
- import java.util.ArrayList;
- import java.util.Set;
- import java.util.HashSet;
- public class lzwCode {
- public static void main(String [] args) {
- testA();
- System.out.println("===========================");
- testB();
- System.out.println("===========================");
- testC();
- }
- //去掉数组中重复的值
- public static void testA() {
- String [] str = {"Java", "C++", "Php", "C#", "Python", "C++", "Java"};
- for (String elementA:str ) {
- System.out.print(elementA + " ");
- }
- List<String> list = new ArrayList<String>();
- for (int i=0; i<str.length; i++) {
- if(!list.contains(str[i])) {
- list.add(str[i]);
- }
- }
- /*
- Set<String> set = new HashSet<String>();
- for (int i=0; i<str.length; i++) {
- set.add(str[i]);
- }
- String[] newStr = set.toArray(new String[1]);
- */
- System.out.println();
- String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组
- for (String elementB:newStr ) {
- System.out.print(elementB + " ");
- }
- System.out.println();
- }
- //删除数组中其中一个元素
- public static void testB() {
- String [] str = {"Java", "C++", "Php", "C#", "Python"};
- for (String elementA:str ) {
- System.out.print(elementA + " ");
- }
- //删除php
- List<String> list = new ArrayList<String>();
- for (int i=0; i<str.length; i++) {
- list.add(str[i]);
- }
- list.remove(2); //list.remove("Php")
- System.out.println();
- String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组
- for (String elementB:newStr ) {
- System.out.print(elementB + " ");
- }
- System.out.println();
- }
- //在数组中增加一个元素
- public static void testC() {
- String [] str = {"Java", "C++", "Php", "C#", "Python"};
- for (String elementA:str ) {
- System.out.print(elementA + " ");
- }
- //增加ruby
- List<String> list = new ArrayList<String>();
- for (int i=0; i<str.length; i++) {
- list.add(str[i]);
- }
- list.add(2, "ruby"); //list.add("ruby")
- System.out.println();
- String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组
- for (String elementB:newStr ) {
- System.out.print(elementB + " ");
- }
- System.out.println();
- }
- }
控制台结果:
——————————————————————————————————————————————————————————————————