码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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)空间复杂度的实现,和题解略有区别

    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;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    Autojs微信研究:微信自动发送信息机器人最终成品(有效果演示)
    图像处理黑科技—破解文档识别难题(PS检测、弯曲拉平、切边切片、摩尔纹)
    华为数通方向HCIP-DataCom H12-821题库(单选题:361-380)
    Hdu 3549 Flow Problem(最大流)
    pycharm远程连接服务器的使用(自用)
    Android 运行报错:Circular dependencies cannot exist in RelativeLayout
    【Java基础】· 集合习题详解
    多线程——信号量
    Java之泛型系列--构造方法使用泛型(有示例)
    JavaScript笔记9-节点操作
  • 原文地址:https://blog.csdn.net/weixin_46841376/article/details/133840313
  • 最新文章
  • C# 内存安全性的重大演进:重新定义 unsafe 关键字
    [MAF的Agent管道详解-05]对话历史的持久化和输入输出的增强
    一行代码干翻 Java 反射?EggG 流式反射调用让反射优雅到不可思议
    vibe coding(二)Where you go:一个微型 windows 桌面覆盖工具
    [送码] 用 AI Coding 做了一个 App,谈谈 AI Coding 的真实体验
    面试官:说一下 Agent 的常见范式,如何选型?
    CAD子系统,是自研还是外包?
    polygon出题教程
    Manim物理模拟:别自己写欧拉了!
    AI 学习笔记:Agent 的应用演示
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号