当前位置:网站首页>Sword finger offer 05 Replace spaces
Sword finger offer 05 Replace spaces
2022-06-22 23:39:00 【Front end plasterer】
One algorithm a day ~~, Stick to the first day .
Please implement a function , Put the string s Replace each space in with "%20".
Range :0 <= s The length of <= 10000
Example :
Input :s = "We are happy."
Output :"We%20are%20happy."
How to solve the problem 1:
Ideas : Simple and rough direct use split Method , Separate them into string arrays with spaces , And then use join Method is then converted to a string .
function replaceSpace(s){
if(s.length > 10000) return null;
return s.split(" ").join("%20");
}
How to solve the problem 2:
Ideas : Replace with regular expressions
function replaceSpace(s){
if(s.length > 10000) return null;
return s.replace(/ /g, '%20')
}
How to solve the problem 3:
Ideas : Loop string , Then determine whether there is a blank space , If there are spaces, replace them with
function replaceSpace(s){
if(s.length > 10000) return null;
let str = ''
for (const key in s) {
if(s[key] === ' '){
str = str + '%20'
}else{
str = str + s[key]
}
}
return str;
}
Now think of these methods . If there is still a good way , I hope you can share more .
Code Not just for young people , It should be a lifelong hobby ~
边栏推荐
- 2021-04-14
- After passing the hcip exam, I still failed to change my career. What do professional network workers value most
- OJ daily practice - filter extra spaces
- 对 cookie 的添加/获取和删除
- js防止PC端复制正确的链接
- LeetCode_ Backtracking_ Dynamic programming_ Medium_ 131. split palindrome string
- Future alternatives to IPv4! Read the advantages, features and address types of IPv6
- Leakcanary source code (2)
- Learn redis with you (11) -- redis distributed lock
- js图片分辨率压缩
猜你喜欢

Customize multi-level list styles in word

Introduction to database access tools

c语言---17 函数简介

C language -- 17 function introduction

2021-04-14

Do domestic mobile phones turn apples? It turned out that it was realized by 100 yuan machine and sharp price reduction

别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!

【Kubernetes 系列】Kubernetes 概述

OJ每日一练——跨越2020

企业数字化不是各自发展,而是全面SaaS化推进
随机推荐
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
ArcGIS application (20) the ArcGIS grid image symbol system prompts "this dataset does not have valid histogram required for classificati..."
剑指 Offer 06. 从尾到头打印链表
Is it safe to open a securities account by downloading the qiniu app? Is there a risk?
2021-08-21
2021-07-27
Tp5.1 solving cross domain problems
IPV4的未来替代品!一文读懂IPV6的优势特点和地址类型
Enterprise digitalization is not a separate development, but a comprehensive SaaS promotion
防抖&节流 加强版
Redis缓存
异步FIFO
2021-08-22
企业数字化不是各自发展,而是全面SaaS化推进
2020-12-04
ArcGIS应用(二十)Arcgis 栅格图像符号系统提示“This dataset does not have valid histogram required for classificati…”
冒泡排序 指针
反向代理HAProxy
OJ daily practice - delete word suffixes
Use the find command