JZ05 [剑指 Offer 05] 替换空格

替换空格

CategoryDifficultyLikesDislikes
lcofEasy (75.74%)341-

 

请实现一个函数,把字符串 s 中的每个空格替换成"%20"。

示例 1:

输入:s = "We are happy."
输出:"We%20are%20happy."

限制:

0 <= s 的长度 <= 10000


Discussion | Solution

分析:

使用replace函数将空格替换为%20

知识点:

basic_string<CharType, Traits, Allocator>& replace(
    size_type position_1,
    size_type number_1,
    const value_type* ptr);

basic_string<CharType, Traits, Allocator>& replace(
    size_type position_1,
    size_type number_1,
    const basic_string<CharType, Traits, Allocator>& str);

basic_string<CharType, Traits, Allocator>& replace(
    size_type position_1,
    size_type number_1,
    const value_type* ptr,
    size_type number_2);

basic_string<CharType, Traits, Allocator>& replace(
    size_type position_1,
    size_type number_1,
    const basic_string<CharType, Traits, Allocator>& str,
    size_type position_2,
    size_type number_2);

basic_string<CharType, Traits, Allocator>& replace(
    size_type position_1,
    size_type number_1,
    size_type count,
    value_type char_value);

basic_string<CharType, Traits, Allocator>& replace(
    iterator first0,
    iterator last0,
    const value_type* ptr);

basic_string<CharType, Traits, Allocator>& replace(
    iterator first0,
    iterator last0,
    const basic_string<CharType, Traits, Allocator>& str);

basic_string<CharType, Traits, Allocator>& replace(
    iterator first0,
    iterator last0,
    const value_type* ptr,
    size_type number_2);

basic_string<CharType, Traits, Allocator>& replace(
    iterator first0,
    iterator last0,
    size_type number_2,
    value_type char_value);

template <class InputIterator>
basic_string<CharType, Traits, Allocator>& replace(
    iterator first0,
    iterator last0,
    InputIterator first,
    InputIterator last);

basic_string<CharType, Traits, Allocator>& replace(
    iterator first0,
    iterator last0,
    const_pointer first,
    const_pointer last);

basic_string<CharType, Traits, Allocator>& replace(
    iterator first0,
    iterator last0,
    const_iterator first,
    const_iterator last);

参数

str
要作为操作数字符串字符的源的字符串。

position_1
开始进行替换的操作数字符串的索引。

number_1
操作数字符串中要替换的最大字符数。

position_2
开始进行复制的参数字符串的索引。

number_2
要从参数 C 字符串使用的最大字符数。

ptr
要作为操作数字符串字符的源的 C 字符串。

char_value
要复制到操作数字符串的字符。

first0
一种迭代器,用于寻址操作数字符串中要删除的第一个字符。

last0
一种迭代器,用于寻址操作数字符串中要删除的最后一个字符。

first
一种迭代器(const_pointer 或 const_iterator),用于寻址参数字符串中进行复制的第一个字符。

last
一种迭代器(const_pointer 或 const_iterator),用于寻址参数字符串中进行复制的最后一个字符。

count
char_value 复制到操作数字符串的次数。
返回值

进行了替换的操作数字符串。

 

代码:

/*
 * @lc app=leetcode.cn id=100280 lang=cpp
 *
 * [剑指 Offer 05] 替换空格
 */

// @lc code=start
class Solution {
public:
    string replaceSpace(string s) {
        
        for(int i = 0; i < s.length(); i++)
            if(s[i] == ' '){
                s.replace(i, 1, "%20");
            }
        return s;
    }
};
// @lc code=end

Accepted

  • 27/27 cases passed (0 ms)
  • Your runtime beats 100 % of cpp submissions
  • Your memory usage beats 10.31 % of cpp submissions (6.2 MB)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值