443 String Compression 压缩字符串

给定一组字符,使用原地算法将其压缩。
压缩后的长度必须始终小于或等于原数组长度。
数组的每个元素应该是长度为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。
详见:https://leetcode.com/problems/string-compression/description/
C++:

class Solution {
public:
    int compress(vector<char>& chars) {
        int n = chars.size(), cur = 0;
        for (int i = 0, j = 0; i < n; i = j)
        {
            while (j < n && chars[j] == chars[i])
            {
                ++j;
            }
            chars[cur++] = chars[i];
            if (j - i == 1)
            {
                continue;
            }
            for (char c : to_string(j - i))
            {
                chars[cur++] = c;
            }
        }
        return cur;
    }
};

参考:https://www.cnblogs.com/grandyang/p/8742564.html

原文地址:https://www.cnblogs.com/xidian2014/p/8900547.html

时间: 2024-10-05 13:36:42

443 String Compression 压缩字符串的相关文章

443. String Compression - LeetCode

Question 443.?String Compression Solution 题目大意:把一个有序数组压缩, 思路:遍历数组 Java实现: public int compress(char[] chars) { if (chars.length == 0) return 0; StringBuilder sb = new StringBuilder(); char cur = chars[0]; int sum = 1; for (int i = 1; i <= chars.length

443. String Compression 字符串压缩

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 inpu

443. String Compression

问题描述: 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 th

【leetcode】443. String Compression

题目如下: 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 th

Leetcode 443 String Compression

基础的字符串操作,用两个指针扫一遍就行. class Solution(object): def compress(self,chars): """ :type chars: List[str] :rtype: int """ if not chars: return 0 if len(chars) == 1: return 1 left, right, num = 0, 1, 1 while right <= len(chars): if

[LC] 443. String Compression

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 inpu

LeetCode_443. String Compression

443. String Compression Easy 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 yo

codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: dp[i]表示前i个字符需要的最小次数. dp[i] = min(dp[j]+w(j+1,i)); (0<=j<i); [j+1,i]如果存在循环节(自身不算),那么取最小的循环节x.w = digit((i-j)/x)+x; 否则w = i-j+1; 求一个区间最小循环节: 证明:http://w

[CareerCup] 1.5 Compress String 压缩字符串

1.5 Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your met