Skip to content

Latest commit

 

History

History
49 lines (37 loc) · 1.06 KB

jz_offer_05_1.md

File metadata and controls

49 lines (37 loc) · 1.06 KB
title description keywords
5. 替换空格
LeetCode 5. 替换空格题解,替换空格,包含解题思路、复杂度分析以及完整的 JavaScript 代码实现。
LeetCode
5. 替换空格
替换空格
替换空格
解题思路
字符串

5. 替换空格

🟢 Easy  🔖  字符串  🔗 力扣

题目

假定一段路径记作字符串 path,其中以 "." 作为分隔符。现需将路径加密,加密方法为将 path 中的分隔符替换为空格 " ",请返回加密后的字符串。

Example:

输入:path = "a.aef.qerf.bb"

输出:"a aef qerf bb"

Constraints:

  • 0 <= path.length <= 10000

解题思路

遍历字符串,将所有的 "." 替换成 " "即可。

代码

/**
 * @param {string} path
 * @return {string}
 */
var pathEncryption = function (path) {
	let res = '';
	for (let i of path) {
		res += i === '.' ? ' ' : i;
	}
	return res;
};