Easy
4461416FavoriteShare
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
.
借助辅助向量法:
c++:
/*
* @Autor: SourDumplings
* @Date: 2019-09-25 19:52:39
* @Link: https://github.com/SourDumplings/
* @Email: changzheng300@foxmail.com
* @Description: https://leetcode.com/problems/string-compression/
*/
class Solution
{
public:
int compress(vector<char> &chars)
{
int count = 0;
vector<char> res;
char lastChar = 0;
for (char c : chars)
{
if (c == lastChar)
{
++count;
}
else
{
if (count == 1)
{
res.push_back(lastChar);
}
else if (count > 1)
{
res.push_back(lastChar);
string num = to_string(count);
for (auto &&cn : num)
{
res.push_back(cn);
}
}
count = 1;
lastChar = c;
}
}
if (count == 1)
{
res.push_back(lastChar);
}
else if (count > 1)
{
res.push_back(lastChar);
string num = to_string(count);
for (auto &&cn : num)
{
res.push_back(cn);
}
}
chars = std::move(res);
return chars.size();
}
};
Java:
import java.util.ArrayList;
import java.util.List;
/*
* @Autor: SourDumplings
* @Date: 2019-09-25 20:10:59
* @Link: https://github.com/SourDumplings/
* @Email: changzheng300@foxmail.com
* @Description: https://leetcode.com/problems/string-compression/
*/
class Solution
{
private int count = 0;
private char lastChar = (char) 0;
public int compress(char[] chars)
{
List<Character> res = new ArrayList<>();
for (char c : chars)
{
if (c == lastChar)
{
++count;
}
else
{
record(lastChar, res);
count = 1;
lastChar = c;
}
}
record(lastChar, res);
int l = res.size();
int i = 0;
for (char c : res)
{
chars[i++] = c;
}
return l;
}
void record(char c, List<Character> res)
{
if (count == 1)
{
res.add(c);
}
else if (count > 1)
{
res.add(c);
String num = String.valueOf(count);
int l = num.length();
for (int i = 0; i < l; i++)
{
char d = num.charAt(i);
res.add(d);
}
}
}
}
就地法:
Java:
/*
* @Autor: SourDumplings
* @Date: 2019-09-25 20:36:42
* @Link: https://github.com/SourDumplings/
* @Email: changzheng300@foxmail.com
* @Description: https://leetcode.com/problems/string-compression/
*/
class Solution
{
public int compress(char[] chars)
{
int count = 0;
char lastChar = (char) 0;
int i = 0;
for (char c : chars)
{
if (c == lastChar)
{
++count;
}
else
{
if (count == 1)
{
chars[i++] = lastChar;
}
else if (count > 1)
{
chars[i++] = lastChar;
String num = String.valueOf(count);
int l = num.length();
for (int j = 0; j < l; j++)
{
char d = num.charAt(j);
chars[i++] = d;
}
}
count = 1;
lastChar = c;
}
}
if (count == 1)
{
chars[i++] = lastChar;
}
else if (count > 1)
{
chars[i++] = lastChar;
String num = String.valueOf(count);
int l = num.length();
for (int j = 0; j < l; j++)
{
char d = num.charAt(j);
chars[i++] = d;
}
}
return i;
}
}