当前位置: 首页 > article >正文

剑指offer 字符串 持续更新中...

文章目录

  • 1. 替换空格
    • 1.1 题目描述
    • 1.2 从前向后替换空格
    • 1.3 从后向前替换空格

持续更新中…

1. 替换空格

替换空格

1.1 题目描述

题目描述:将一个字符串s中的每个空格替换成“%20”。

示例

输入:"We Are Happy"
返回:"We%20Are%20Happy"

1.2 从前向后替换空格

时间复杂度是O(n^2)

#include <string>
class Solution {
    std::string res;        // 要返回的字符串
    int n, blank_cnt;       // res的长度, 空格的个数
  public:
    string replaceSpace(string s) {
        res = s;
        blank_cnt = countBlank(s);
        res.resize(s.size() + blank_cnt * 2);
        n = res.size();
        for (int i = 0; i < n; ++i) {
            if (res[i] == ' ') {
                moveStr(i);
                res[i]     = '%';
                res[i + 1] = '2';
                res[i + 2] = '0';
            }
        }
        return res;
    }

    // 从pos+1开始,将每个字符向后移动两次
    void moveStr(int pos) {
        for (int i = n; i >= pos + 1; --i) {
            res[i + 2] = res[i];
        }
    }

    // 得到空格的个数
    int countBlank(string s) {
        int cnt = 0;
        for (int i = 0; i < s.size(); ++i)
            if (res[i] == ' ')
                cnt++;
        return cnt;
    }
};

1.3 从后向前替换空格

时间复杂度是O(n)

#include <cstddef>
#include <string>
#include <type_traits>
class Solution {
    std::string res;        // 要返回的字符串
    size_t n, blank_cnt;    // res的长度, 空格的个数
    int left, right;        // 左右指针
  public:
    string replaceSpace(string s) {
        res = s;
        blank_cnt = countBlank(s);
        res.resize(s.size() + blank_cnt * 2);
        n = res.size();
        left = s.size(), right = n;
        while (left < right) {
            if (res[left] == ' ') {
                left--;
                res[right--] = '0';
                res[right--] = '2';
                res[right--] = '%';
            } else {
                res[right] = res[left];
                right--, left--;
            }
            if (left == right || left < 0)    break;
        }
        std::cout << res;
        return res;
    }

    // 得到空格的个数
    int countBlank(string s) {
        int cnt = 0;
        for (int i = 0; i < s.size(); ++i)
            if (res[i] == ' ')
                cnt++;
        return cnt;
    }
};

这题88. 合并两个有序数组 - 力扣(LeetCode)与之类似,我们可以得到一个结论:合并两个字符串(包括数组),如果从前往后复制,可能重复移动,那么就可以考虑从后往前复制


http://www.kler.cn/a/527637.html

相关文章:

  • HTML(快速入门)
  • 绝对值线性化
  • openssl 生成证书 windows导入证书
  • 【Linux】线程互斥与同步
  • 【Rust自学】14.6. 安装二进制crate
  • 本地部署deepseek模型步骤
  • 使用Pygame制作“动态烟花”
  • 基于互联网+智慧水务信息化整体解决方案
  • unity学习25:用 transform 进行旋转和移动,简单的太阳地球月亮模型,以及父子级关系
  • 【Pandas】pandas Series diff
  • C语言指针专题二 -- 字符指针与字符串
  • 翻译: Anthropic CEO:DeepSeek-R1是人工智能领域的革命吗?二
  • 一文读懂fgc之cms
  • web安全测试之 xss攻击_request
  • [openwrt] odhcpd ra_management Vs ra_flags 和 ra_slaac
  • 守护进程
  • 代码随想录34 动态规划
  • C动态库的生成与在Python和QT中的调用方法
  • 猿人学web 19题(js逆向)
  • 为AI聊天工具添加一个知识系统 之70 详细设计 之11 维度运动控制的应用:上下文受控的自然语言
  • Git进阶之旅:Git 分支管理
  • gcc和g++的区别以及明明函数有定义为何链接找不到
  • 计算机网络——流量控制
  • CSS 溢出内容处理:从基础到实战
  • 解锁豆瓣高清海报(一) 深度爬虫与requests进阶之路
  • [EAI-029] RoboVLMs,基于VLM构建VLA模型的消融研究