1. 程式人生 > >443. String Compression(python+cpp)

443. String Compression(python+cpp)

題目:

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: All characters have an ASCII value in [35, 126]. 1 <= len(chars) <= 1000.

解釋: 原地壓縮字串。原地的意思就是不要使用額外空間,我原本寫時候用額外空間儲存之後又賦值給原陣列是幾個意思? python程式碼:

class Solution(object):
    def compress(self, chars):
        """
        :type chars: List[str]
        :rtype: int
        """
        index=0
        count=1
        i=1
        while i<len(chars):
            if chars[i]==chars[index]:
                count+=1
            else:
                len_count=len(str(count))
                if count>1:
                    for j in range(len_count):
                        chars[index+j+1]=str(count)[j]
                    index+=len_count
                index+=1
                count=1
                chars[index]=chars[i]
            i+=1
        len_count=len(str(count))
        if count>1:
            for j in range(len_count):
                chars[index+j+1]=str(count)[j]
            index+=len_count
        index+=1
        return index      

c++程式碼:

class Solution {
public:
    int compress(vector<char>& chars) {
        int n =chars.size();
        int count=1;
        int i=1;
        int index=0;
        while (i<n)
        {
            if(chars[i]==chars[index])
                count++;
            else
            {
                string count_str=to_string(count);
                int len_count_str=count_str.size();
                if (count>1)
                {
                    for (int j=0;j<len_count_str;j++)
                        chars[index+1+j]=count_str[j];
                    index+=len_count_str;
                    
                }  
                index+=1;
                count=1;
                chars[index]=chars[i];
            }   
            i++;
        }
        string count_str=to_string(count);
        int len_count_str=count_str.size();
        if (count>1)
        {
            for (int j=0;j<len_count_str;j++)
                chars[index+1+j]=count_str[j];
            index+=len_count_str;
        }  
        index+=1;
        return index;
    }
};

總結: 對於這種原地修改的題目,一般都是要一個index指向已經修改好的部分的末尾的。