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:
Example 1:
Example 2:
Example 3:
Note:
All characters have an ASCII value in [35, 126].
1 <= len(chars) <= 1000.
解法1: in-place
inplace的解法就是用一个pos记录当前需要插入的位置。
用一个变量last记录上一个字符,用count记录那个字符重复的次数。
如果当前字符和last一样则更新count,否则的话就写入到array中。
|
|