剑指 Offer 05. 替换空格
字符串替换
题目链接-来源:力扣(LeetCode)
请实现一个函数,把字符串 s 中的每个空格替换成”%20”。
示例 1:
输入:s = “We are happy.”
输出:”We%20are%20happy.”
思路:
字符串替换,可以用 StringBuilder 类轻松实现。对空格所在的指针 i 进行删除与插入操作即可。
时间复杂度:O(n)
空间复杂度:O(n)
实现:
1 | class Solution { |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.