leetcode 443. String Compression 压缩字符串+暴力遍历
2017-12-09 15:35
465 查看
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.
就是压缩字符串,使用两个指针即可完成,一个指针做遍历,另一个做结果的遍历
代码如下:
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.
就是压缩字符串,使用两个指针即可完成,一个指针做遍历,另一个做结果的遍历
代码如下:
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <cmath> using namespace std; class Solution { public: int compress(vector<char>& c) { int start = 0 , end = 0; int count = 0; for (int end = 0; end < c.size(); end++) { count++; if (end == c.size() - 1 || c[end] != c[end + 1]) { c[start++] = c[end]; if (count > 1) { string tmp = to_string(count); for (char t : tmp) c[start++] = t; } count = 0; } } return start; } };
相关文章推荐
- leetcode 443. String Compression(字符串压缩)
- leetcode之压缩字符串中的重复字符
- leetcode 692. Top K Frequent Words 前K个重复出现的字符串+Map遍历统计
- Leetcode 336. Palindrome Pairs 给出一种遍历字符串回文子串中心的方法
- LeetCode 443. String Compression (压缩字符串)
- [LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器
- leetcode 97. Interleaving String(字符串交错出现) DFS深度优先遍历 + 很明显很经典的DP动态规划做法
- leetcode_434. Number of Segments in a String 计算字符串中段的个数,只需遍历一遍字符串
- leetcode 699. Falling Squares 俄罗斯方块的最高高度 + 暴力遍历即可
- leetcode 336. Palindrome Pairs 回文数字符串对 + Map加速暴力查询
- leetcode 394. Decode String 字符串解码 + DFS深度优先遍历求解
- 利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能。比如,字符串“aabcccccaaa”经压缩会变成“a2b1c5a3”。若压缩后的字符串没有变短,则返回原先的字符串。
- Java遍历文件夹下所有文件并替换指定字符串
- HW机试字符串压缩java(1)
- 【题解】【字符串】【BFS】【Leetcode】Word Ladder
- 每天一道LeetCode-----在字符串s中找到最短的包含字符串t中所有字符的子串,子串中字符顺序无要求且可以有其他字符
- L1-023 输出GPLT(字符串的筛选遍历)
- 字符串查找算法总结(暴力匹配、KMP 算法、Boyer-Moore 算法和 Sunday 算法)
- [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
- 从C++遍历路径中含有汉字的文件夹看宽窄字符串转换的问题