LeetCode75——Day9

文章目录

一、题目

443. String Compression

Given an array of characters chars, compress it using the following algorithm:

Begin with an empty string s. For each group of consecutive repeating characters in chars:

If the group's length is 1, append the character to s.

Otherwise, append the character followed by the group's length.

The compressed string s should not be returned separately, but instead, be stored in the input character array chars. Note that group lengths that are 10 or longer will be split into multiple characters in chars.

After you are done modifying the input array, return the new length of the array.

You must write an algorithm that uses only constant extra space.

Example 1:

Input: chars = ["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: The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".

Example 2:

Input: chars = ["a"]

Output: Return 1, and the first character of the input array should be: ["a"]

Explanation: The only group is "a", which remains uncompressed since it's a single character.

Example 3:

Input: chars = ["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: The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".

Constraints:

1 <= chars.length <= 2000

chars[i] is a lowercase English letter, uppercase English letter, digit, or symbol.

二、题解

O(n)时间复杂度,O(1)空间复杂度的实现,和题解略有区别

cpp 复制代码
class Solution {
public:
    int compress(vector<char>& chars) {
        int n = chars.size();
        int index = 0, fast = 0;
        while(fast < n){
            char curChar = chars[fast];
            int curIndex = fast;
            while(fast < n && chars[fast] == curChar) fast++;
            int gap = fast - curIndex;
            if(gap == 1) chars[index++] = chars[curIndex];
            else{
                chars[index++] = chars[curIndex];
                string tmp = to_string(gap);
                for(int i = 0;i < tmp.length();i++) chars[index + i] = tmp[i];
                index += tmp.length();
            }
        }
        return index;
    }
};
相关推荐
夏鹏今天学习了吗3 分钟前
【LeetCode热题100(64/100)】搜索旋转排序数组
算法·leetcode·职场和发展
alphaTao21 分钟前
LeetCode 每日一题 2025/11/3-2025/11/9
windows·leetcode
2301_7965125224 分钟前
Rust编程学习 - 问号运算符会return一个Result 类型,但是如何使用main函数中使用问号运算符
开发语言·学习·算法·rust
草莓熊Lotso29 分钟前
Linux 基础开发工具入门:软件包管理器的全方位实操指南
linux·运维·服务器·c++·人工智能·网络协议·rpc
小龙报40 分钟前
算法通关指南:数据结构和算法篇 --- 队列相关算法题》--- 1. 【模板】队列,2. 机器翻译
c语言·开发语言·数据结构·c++·算法·学习方法·visual studio
晨非辰1 小时前
【数据结构初阶】--从排序算法原理分析到代码实现操作,参透插入排序的奥秘!
c语言·开发语言·数据结构·c++·算法·面试·排序算法
三川6982 小时前
排序算法介绍
数据结构·算法·排序算法
2301_795167205 小时前
玩转Rust高级应用 如何避免对空指针做“解引用”操作,在C/C++ 里面就是未定义行为
c语言·c++·rust
智驱力人工智能6 小时前
基于视觉分析的人脸联动使用手机检测系统 智能安全管理新突破 人脸与手机行为联动检测 多模态融合人脸与手机行为分析模型
算法·安全·目标检测·计算机视觉·智能手机·视觉检测·边缘计算
2301_764441337 小时前
水星热演化核幔耦合数值模拟
python·算法·数学建模