算法空间/Leetcode/替换空格:修订间差异
来自康健生活
无编辑摘要 |
|||
| 第6行: | 第6行: | ||
# 输出 <code>We%20are%20happy.</code> | # 输出 <code>We%20are%20happy.</code> | ||
== 实现 == | === 实现 === | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
var pathEncryption = function (path) { | var pathEncryption = function (path) { | ||
| 第16行: | 第16行: | ||
};</syntaxhighlight> | };</syntaxhighlight> | ||
== 结论 == | === 结论 === | ||
打开 leetcode 发现本题已经被替换为 [https://leetcode.cn/problems/ti-huan-kong-ge-lcof/description/ LCR 122. 路径加密],本实现的解题思路是利用<code>不断重置引用地址的存储内容</code>,复杂度是 <math>O(N)</math>。 | 打开 leetcode 发现本题已经被替换为 [https://leetcode.cn/problems/ti-huan-kong-ge-lcof/description/ LCR 122. 路径加密],本实现的解题思路是利用<code>不断重置引用地址的存储内容</code>,复杂度是 <math>O(N)</math>。 | ||
2026年1月25日 (日) 12:08的版本
题目描述
实现一个函数,把字符串中的每个空格替换成 %20。
示例
- 输入
We are happy. - 输出
We%20are%20happy.
实现
var pathEncryption = function (path) {
let cup = ''
for (let i = path.length - 1; i >= 0; i--) {
cup = (path[i] === ' ' ? '%20' : path[i]) + cup
}
return cup
};
结论
打开 leetcode 发现本题已经被替换为 LCR 122. 路径加密,本实现的解题思路是利用不断重置引用地址的存储内容,复杂度是 。
执行用时分布 0ms 消耗内存分布 54.09MB
No categories