来源:力扣 (LeetCode)
链接:https://leetcode-cn.com/problems/compress-string-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
一、题目描述
字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串 aabcccccaaa 会变为 a2b1c5a3 。若 「压缩」 后的字符串没有变短,则返回原先的字符串。你可以假设字符串中只包含大小写英文字母 (a 至 z) 。
示例 1:
- 输入:"aabcccccaaa"
- 输出:"a2b1c5a3"
示例 2:
- 输入:"abbccd"
- 输出:"abbccd"
- 解释:"abbccd"压缩后为"a1b2c2d1",比原字符串长度更长。
提示:字符串长度在 [0, 50000] 范围内。
二、题解
一道很简单的字符串处理题型,思路:
- 使用一个计数器保存每个重复字符的个数。
- 遇到相同字符,计数加一。
- 遇到不同字符,把前面的字符和计数加到输出字符串末尾,计数器归零。
三、代码
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
class Solution { public: string compressString(string S) { char ch; int i, cnt; string output; stringstream ss; if (S.size() <= 1) return S; cnt = 1; ch = S[0]; for (i = 1; i < S.size(); i++) { if (S[i] == ch) { cnt++; } else { // 压入到字符串流 ss << ch << cnt; // 重新计数 cnt = 1; ch = S[i]; } } // 最后一个字符和计数也放到字符串流里面去 ss << ch << cnt; // 打印到输出字符串 ss >> output; return output.size() < S.size() ? output : S; } }; |
![【每日打卡】[leetcode]面试题1.6-字符串压缩](https://www.dyxmq.cn/wp-content/uploads/2020/03/776bc-image.png)

![[leetcode]887-鸡蛋掉落](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/b0c9f-imageba57ef224a3508f7.md.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]72-编辑距离](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/88c60-imageff84a6c5047db6ed.png&w=280&h=210&a=&zc=1)
![[leetcode]556-下一个更大的元素III](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/7119d-image968307faea0b8cac.png&w=280&h=210&a=&zc=1)

![[leetcode]199-二叉树的右视图](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/f24a8-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]460-LFU缓存](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/5b3f2-image.png&w=280&h=210&a=&zc=1)
![[leetcode]125-验证回文串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/5bfa5-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[程序员面试宝典]17.16-按摩师](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/9ecc9-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]876-链表的中间节点](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/64d0d-imagece778ab033c9d90d.png&w=280&h=210&a=&zc=1)
![【每日打卡】[剑指offer]面试题40-最小的k个数](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/c83c9-imagec745c425c670e18c.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]-409最长回文子串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/e5589-image788d511d7c1e839c.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]695-岛屿的最大面积](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/aa889-image036941cd6bbf2296.png&w=280&h=210&a=&zc=1)
评论