当前位置:网站首页>~4.1 sword finger offer 05. replace spaces
~4.1 sword finger offer 05. replace spaces
2022-07-25 14:04:00 【Ye Xiaobai】
The finger of the sword Offer 05. Replace blank space
Title Description

Ideas
C++:
stay C++ In language , string Designed to 「 variable 」 The type of , Therefore, you can modify in place without creating a new string .
Ideas :
(1)1 Spaces become %20, The length will increase 2.
— So first count the number of spaces , Then change string The length of
(2)i Point to the end of the original string ,j Point to the end of the current string , ij Represents that there is no space left of the original string
— If s[i]!=’ ’ that s[j]=s[i]
— If s[i]’ ’ Then close the string [j-2, j] The element of is changed to “%20”,j=j-2
I know for the first time string It's actually of variable length , Originally, my problem is that I don't know the length of the input string , Therefore, the length of the array cannot be determined , Take it at last vector Realized ,vector Implementation is similar to java,python, You don't need to know the length in advance . The idea is very simple , If it's a space, add it ’%20’, Instead of adding the original string .
Source code
C++
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
string replaceSpace(string s) {
//vector The function is void That's it
vector<char > v;
for (char c : s) {
if (c==' '){
v.push_back('%');
v.push_back('2');
v.push_back('0');
} else{
v.push_back(c);
}
}
for (int i = 0; i <v.size() ; i++) {
cout<<v[i];
}
cout<<endl;
// change string Length solution :
int count = 0, len = s.size();
// Count the number of spaces
for (char c : s) {
if (c == ' ') count++;
}
// modify s length
s.resize(len + 2 * count);
// Traversal modification in reverse order
for(int i = len - 1, j = s.size() - 1; i < j; i--, j--) {
if (s[i] != ' ')
s[j] = s[i];
else {
s[j - 2] = '%';
s[j - 1] = '2';
s[j] = '0';
j -= 2;
}
}
return s;
}
};
int main (){
Solution solution;
string s="We are friend!";
string re=solution.replaceSpace(s);
cout<<re<<endl;
return 0;
}
java:
class Solution {
public String replaceSpace(String s) {
StringBuilder res = new StringBuilder();
for(Character c : s.toCharArray())
{
if(c == ' ') res.append("%20");
else res.append(c);
}
return res.toString();
}
}
边栏推荐
- R语言如何将大型Excel文件转为dta格式详解
- leetcode202---快乐数
- Vscode plug-in development
- Feiwo technology IPO meeting: annual revenue of 1.13 billion Hunan Cultural Tourism and Yuanli investment are shareholders
- What you must know about data engineering in mlops
- Interpretation of featdepth self-monitoring model for monocular depth estimation (Part I) -- paper understanding and core source code analysis
- Amd epyc 9664 flagship specification exposure: 96 core 192 threads 480MB cache 3.8ghz frequency
- 飞盘局有多快乐?2022年轻人新潮运动报告
- IDEA设置提交SVN时忽略文件配置
- Brush questions - Luogu -p1085 unhappy Jinjin
猜你喜欢

Vscode plug-in development

Business data analysis of CDA level1 knowledge point summary

Workplace "digital people" don't eat or sleep 007 work system, can you "roll" them?

2271. Maximum number of white bricks covered by blanket ●●

在线问题反馈模块实战(十三):实现多参数分页查询列表

Leetcode -- addition of four numbers II

飞盘局有多快乐?2022年轻人新潮运动报告

【配置Hifive1-revB】设备管理器中不识别端口,Can not connect to J-Link via USB的解决办法

应急科普|收好这份暑期安全指南,让孩子安全过暑假!
![Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component](/img/39/6f6760e80acec0b02028ea2ed1a5b1.png)
Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component
随机推荐
Applet sharing function
手里有点钱可以投资哪些理财产品?
埃拉托斯特尼筛法
CDA level Ⅰ 2021 new version simulation question 2 (with answers)
Brush questions - luogu-p1089 Jinjin savings plan
Nodejs link MySQL error: Er_ NOT_ SUPPORTED_ AUTH_ MODEError: ER_ NOT_ SUPPORTED_ AUTH_ MODE
Workplace "digital people" don't eat or sleep 007 work system, can you "roll" them?
Leetcode 205. isomorphic string (2022.07.24)
MySQL and Navicat installation and stepping on pits
Tm1637 four digit LED display module Arduino driver with second dot
Digital Twins - cognition
金鱼哥RHCA回忆录:CL210管理存储--管理共享文件系统
Brush questions - Luogu -p1047 trees outside the school gate
Depth estimation self-monitoring model monodepth2 paper summary and source code analysis [theoretical part]
Feiwo technology IPO meeting: annual revenue of 1.13 billion Hunan Cultural Tourism and Yuanli investment are shareholders
OKA通证权益解析,参与Okaleido生态建设的不二之选
Working principle of Lora to 4G and gateway repeater
einsum(): operands do not broadcast with remapped shapes [original->remapped]: [1, 144, 20, 17]->[1,
苹果手机端同步不成功,退出登录,结果再也登录不了了
Brush questions - Luogu -p1146 coin flip