Skip to content

Latest commit

 

History

History
185 lines (141 loc) · 4.39 KB

File metadata and controls

185 lines (141 loc) · 4.39 KB

English Version

题目描述

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

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

数组的每个元素应该是长度为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

解法

双指针。

Python3

class Solution:
    def compress(self, chars: List[str]) -> int:
        i, k, n = 0, 0, len(chars)
        while i < n:
            j = i + 1
            while j < n and chars[j] == chars[i]:
                j += 1
            chars[k] = chars[i]
            k += 1
            if j - i > 1:
                cnt = str(j - i)
                for c in cnt:
                    chars[k] = c
                    k += 1
            i = j
        return k

Java

class Solution {
    public int compress(char[] chars) {
        int k = 0, n = chars.length;
        for (int i = 0, j = i + 1; i < n;) {
            while (j < n && chars[j] == chars[i]) {
                ++j;
            }
            chars[k++] = chars[i];
            if (j - i > 1) {
                String cnt = String.valueOf(j - i);
                for (char c : cnt.toCharArray()) {
                    chars[k++] = c;
                }
            }
            i = j;
        }
        return k;
    }
}

C++

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

Go

func compress(chars []byte) int {
	i, k, n := 0, 0, len(chars)
	for i < n {
		j := i + 1
		for j < n && chars[j] == chars[i] {
			j++
		}
		chars[k] = chars[i]
		k++
		if j-i > 1 {
			cnt := strconv.Itoa(j - i)
			for _, c := range cnt {
				chars[k] = byte(c)
				k++
			}
		}
		i = j
	}
	return k
}

...