【Leetcode】443. String Compression (Easy)

1.题目

Given an array of characters, compress it in-place.

The length after compression must always be smaller than or equal to the original array.

Every element of the array should be a character (not int) of length 1.

After you are done modifying the input array in-place, return the new length of the array.


Follow up:
Could you solve it using only O(1) extra space?


Example 1:

Input:
["a","a","b","b","c","c","c"]

Output:
Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]

Explanation:
"aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".

Example 2:

Input:
["a"]

Output:
Return 1, and the first 1 characters of the input array should be: ["a"]

Explanation:
Nothing is replaced.

Example 3:

Input:
["a","b","b","b","b","b","b","b","b","b","b","b","b"]

Output:
Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].

Explanation:
Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12".
Notice each digit has it's own entry in the array.

Note:

  1. All characters have an ASCII value in [35, 126].
  2. 1 <= len(chars) <= 1000.
2.思路

 超时思路:第一次我想到的也是新建一个数组,但是很想知道在原数组,能否实现。具体思路是,发现与当前char不一致时,统计当前char循环了多少次,更新原数组的值(填入重复次数转换成的char),并进行左移,(紧接数字char)。

如 a,a,a,b,b,c,c 第一次移动后为 a,3,b,b,c,c,c.下一次循环时,并不读取所有字符,而是读取移动后的字符长度。例如对上一个例子来说,a,a,a,b,b,c,c,c是从第一个b开始左移1为得到的,那么循环的终点值也相应减1.

不知道我有没有说清楚,反正这个算法有点凌乱,61/70 case的时候超时了。

 正确思路:新建一个数组res,拷贝当前的char值,当循环遍历发现与当前char值不一致时,将char的重复次数进行转换——如果为1,则不需要进行变动;否则进行转换,如重复了123次,则将'1','2','3'插入res数组的正确位置中.

int转char :char a=(char) (8+48); 即将8(int)转换成了'8'.

3.算法

(如果感兴趣的话,注释掉的是超时的代码)

public static int compress(char[] chars) {
        int n=chars.length;
        if(n==0||n==1)
            return n;
        
//         int res=0;//return this number
//         int repeat=1;//for every element in the arrays
        
//         char temp=chars[0];
//         int temp_head=0;
        
//         int current=1;//current index of the arrays
//         int n1=n;
        
//         while(current<n1){
//             if(chars[current]==temp){
//                 repeat++;
//                 current++;
//             }else{
//                 if(repeat==1){
//                 	System.out.println("repeat ==1");
//                     res+=1;
//                     temp=chars[current];
//                     temp_head=current;
//                     current=current+1;
//                 }
//                 if(repeat!=1){
//                 	System.out.println("repeat !=1");
//                     temp=chars[current];
//                     res=res+1+lenOfChar(repeat);
//                     int len=lenOfChar(repeat);
//                     for(int i=len-1;i>=0;i--){
//                         int c=(int)(repeat/(Math.pow(10,i)));
//                         repeat=(int)(repeat-c*Math.pow(10,i));
//                         chars[temp_head+len-i]=(char) (c + 48);
//                     }
//                     for(int i=current;i<n1;i++){
//                         chars[i-current+temp_head+1+len]=chars[i];
//                     }
//                     for(int i=0;i<n;i++){
//                     	System.out.print(chars[i]);
//                     }
                    
//                     n1=n1-(current-(temp_head+len)-1);
//                     System.out.println();
//                     System.out.println("n1 "+n1);
//                     current=temp_head+len+1;
//                     System.out.println("current "+current);
//                     System.out.println();
//                     repeat=1;
                    
//                     temp_head=current;
//                     current=current+1;
//                 }
                
//             }  
//         }
//         if(repeat==1)
//         	res++;
//         if(repeat!=1){
//         	res=res+1+lenOfChar(repeat);
//         	int len=lenOfChar(repeat);
//         	for(int i=len-1;i>=0;i--){
//         		int c=(int)(repeat/(Math.pow(10,i)));
//         		repeat=(int)(repeat-c*Math.pow(10,i));
//         		chars[temp_head+len-i]=(char) (c + 48);
//         	}
//         }
        
//         for(int i=0;i<n;i++){
//         	System.out.print(chars[i]);
//         }
//         return res;
        
        
        
        char res[]=new char[n];
        res[0]=chars[0];
        int res_index=1;
        
        char temp=chars[0];
        int count=1;
        for(int i=1;i<n;i++){
            if(temp==chars[i]){
                count+=1;
            }else{
                if(count!=1){
                    int len=lenOfChar(count);
                    for(int j=len-1;j>=0;j--){
                        int c=(int)(count/(Math.pow(10,j)));
                        count=(int)(count-c*Math.pow(10,j));
                        res[res_index++]=(char) (c + 48);
                    } 
                    count=1;
                }
                res[res_index++]=chars[i];
                temp=chars[i];     
            }
        }
        if(count!=1){
            int len=lenOfChar(count);
                    for(int j=len-1;j>=0;j--){
                        int c=(int)(count/(Math.pow(10,j)));
                        count=(int)(count-c*Math.pow(10,j));
                        res[res_index++]=(char) (c + 48);
                    }
        }
        for(int i=0;i<n;i++){
            chars[i]=res[i];
        }
        
        return res_index;
    }
    
    private static int lenOfChar(int count){
        int res=1;
        while(count/10!=0){
            res+=1;
            count/=10;
        }
        return res;
    }

4.总结
 高效算法要找到时间复杂度和空间复杂度间的平衡。不能一味追求其中一个,例如我的超时思路,空间复杂度是很低,就在原来的空间进行操作,但多次的移动操作导致时间复杂度太大。得不偿失。
    我是在本地的java环境中测试运行的,加了很多打印语句,在提交时拖慢了运行时间。打印语句去掉前,超过0.62%的朋友,去掉后,超过44%左右。之前用c语言编程中断服务程序时,也是因为加入了打印语句一直出bug。
今天被这道题折磨了好久,睡个好觉,晚安。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值