当前位置:网站首页>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 ~
边栏推荐
- LeakCanary 源码详解(2)
- 防抖&节流 加强版
- C language greedy snake
- 优化——线性规划
- [STM32 skill] use the hardware I2C of STM32 Hal library to drive rx8025t real-time clock chip
- Php7.3 error undefined function simplexml_ load_ string()
- Which securities company is the safest and best choice for stock trading account opening
- eslint 简单配置
- [arm] it is reported that horizontal display is set for LVDS screen of rk3568 development board
- Smart data won two annual awards at the second isig China Industrial Intelligence Conference
猜你喜欢

Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading

China Mobile's mobile phone users grow slowly, but strive for high profit 5g package users

程序员接私活兼职选择

canvas生成海报

three.js模拟驾驶游览艺术展厅---打造超级相机控制器

c# sqlsugar,hisql,freesql orm框架全方位性能测试对比 sqlserver 性能测试

Spark RDD Programming Guide(2.4.3)

【ARM】讯为rk3568开发板lvds屏设置横屏显示

Digital data was invited to participate in Nantong enterprise digital transformation Seminar

2021-08-21
随机推荐
Common operations of sourcetree version management
2020-12-04
Phantomjs utility code snippet (under continuous update...)
剑指 Offer 07. 重建二叉树
14. 最长公共前缀
SOA Service Oriented Architecture
Introduction to database access tools
flink同步mysql数据到ES
Enterprise digitalization is not a separate development, but a comprehensive SaaS promotion
Digital data was invited to participate in Nantong enterprise digital transformation Seminar
OJ daily practice - class dining
对 cookie 的添加/获取和删除
企业数字化不是各自发展,而是全面SaaS化推进
1. class inheritance (point)
Smart data won two annual awards at the second isig China Industrial Intelligence Conference
Leakcanary source code (2)
Using the hbuilder x editor to install a solution for terminal window plug-ins that are not responding
c语言---17 函数简介
WebRTC系列-网络传输之4Connection排序
Spark SQL 访问json和jdbc数据源