LeetCode 443. 压缩字符串(C、C++、python)

给定一组字符,使用原地算法将其压缩。

压缩后的长度必须始终小于或等于原数组长度。

数组的每个元素应该是长度为1 的字符(不是 int 整数类型)。

在完成原地修改输入数组后,返回数组的新长度。

 

进阶:
你能否仅使用O(1) 空间解决问题?

 

示例 1:

输入:
["a","a","b","b","c","c","c"]

输出:
返回6,输入数组的前6个字符应该是:["a","2","b","2","c","3"]

说明:
"aa"被"a2"替代。"bb"被"b2"替代。"ccc"被"c3"替代。

示例 2:

输入:
["a"]

输出:
返回1,输入数组的前1个字符应该是:["a"]

说明:
没有任何字符串被替代。

示例 3:

输入:
["a","b","b","b","b","b","b","b","b","b","b","b","b"]

输出:
返回4,输入数组的前4个字符应该是:["a","b","1","2"]。

说明:
由于字符"a"不重复,所以不会被压缩。"bbbbbbbbbbbb"被“b12”替代。
注意每个数字在数组中都有它自己的位置。

注意:

所有字符都有一个ASCII值在[35, 126]区间内。

1 <= len(chars) <= 1000

C

int compress(char* chars, int charsSize) 
{
    int n=charsSize;
    char* temp=(char*)malloc(sizeof(char)*n);
    int k=0;
    int count=1;
    int ans=0;
    char ss[255];
    for(int i=1;i<n;i++)
    {
        if(chars[i]==chars[i-1])
        {
            count++;
        }
        else
        {
            ans+=1;
            temp[k]=chars[i-1];
            k++;
            if(count!=1)
            {
                sprintf(ss,"%d",count);
                ans+=strlen(ss);
                for(int j=0;j<strlen(ss);j++)
                {
                    temp[k]=ss[j];
                    k++;
                }
            }
            count=1;
        }
    }
    ans+=1;
    temp[k]=chars[n-1];
    k++;
    if(count!=1)
    {
        sprintf(ss,"%d",count);
        ans+=strlen(ss);
        for(int j=0;j<strlen(ss);j++)
        {
            temp[k]=ss[j];
            k++;
        }
    }
    temp[k]='\0';
    strcpy(chars,temp);
    return ans;    
}

C++

class Solution {
public:
    int compress(vector<char>& chars) 
    {
        int n=chars.size();
        int count=1;
        vector<char> temp;
        int ans=0;
        for(int i=1;i<n;i++)
        {
            if(chars[i]==chars[i-1])
            {
                count++;
            }
            else
            {
                ans+=1;
                temp.push_back(chars[i-1]);
                if(count!=1)
                {
                    string s=to_string(count);
                    for(int j=0;j<s.length();j++)
                    {
                        temp.push_back(s[j]);
                    }
                    ans+=s.length();
                }
                count=1;
            }
        }
        ans+=1;
        temp.push_back(chars[n-1]);
        if(count!=1)
        {
            string s=to_string(count);
            for(int j=0;j<s.length();j++)
            {
                temp.push_back(s[j]);
            }
            ans+=s.length();
        }
        chars=temp;
        return ans;
    }
};

C++ O(1)空间

class Solution {
public:
    int compress(vector<char>& chars) 
    {
        int n=chars.size();
        int num=1;
        for(int i=1;i<chars.size();i++)
        {
            if(chars[i]==chars[i-1])
            {
                num+=1;
                chars.erase(chars.begin()+i);
                i--;
            }
            else
            {
                if(num>1)
                {
                    string s=to_string(num);
                    for(auto cc:s)
                    {
                        chars.insert(chars.begin()+i,cc);
                        i++;
                    }
                    num=1;
                }
            }
        }
        if(num>1)
        {
            string s=to_string(num);
            for(auto cc:s)
            {
                chars.push_back(cc);
            }
        }
        return chars.size();
    }
};

python

class Solution:
    def compress(self, chars):
        """
        :type chars: List[str]
        :rtype: int
        """
        n=len(chars)
        temp=[]
        res=0
        count=1
        for i in range(1,n):
            if chars[i]==chars[i-1]:
                count+=1
            else:
                res+=1
                temp.append(chars[i-1])
                if count!=1:
                    s=str(count)
                    for j in range(0,len(s)):
                        temp.append(s[j])
                    res+=len(s)
                count=1
        res+=1
        temp.append(chars[n-1])
        if count!=1:
            s=str(count)
            for j in range(0,len(s)):
                temp.append(s[j])
            res+=len(s)
        for i in range(0,res):
            chars[i]=temp[i]
        return res

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值