当前位置:网站首页>~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();
}
}
边栏推荐
- Applet enterprise red envelope function
- Namespaces and libraries
- Interpretation of featdepth self-monitoring model for monocular depth estimation (Part 2) -- use of openmmlab framework
- Gartner 2022 top technology trend: Super automation
- 2271. 毯子覆盖的最多白色砖块数 ●●
- Vscode plug-in development
- CDA level1 multi topic selection
- Brush questions - Luogu -p1146 coin flip
- Day1:三种语言暴刷牛客130题
- Internal error of LabVIEW
猜你喜欢

Pytorch uses tensorboard to realize visual summary

Okaleido生态核心权益OKA,尽在聚变Mining模式

How happy is the frisbee bureau? 2022 youth trendy Sports Report

Applet starts wechat payment

Practice of online problem feedback module (13): realize multi parameter paging query list

The practice of depth estimation self-monitoring model monodepth2 in its own data set -- single card / multi card training, reasoning, onnx transformation and quantitative index evaluation

Package management apt, dpkg

Sunfeng, general manager of Yixun: the company has completed the share reform and is preparing for IPO

CDA level Ⅰ 2021 new version simulation question 2 (with answers)

ADB connects to Xiaomi mobile phone via Wi Fi
随机推荐
百度搜索打击盗版网文站点,SEOer应该关注哪些问题?
It is predicted that 2021 will accelerate the achievement of super automation beyond RPA
IM系统-消息流化一些常见问题
Common problems in the use of wireless vibrating wire acquisition instrument
ADB connects to Xiaomi mobile phone via Wi Fi
Multidimensional pivoting analysis of CDA level1 knowledge points summary
Sunfeng, general manager of Yixun: the company has completed the share reform and is preparing for IPO
Vscode plug-in development
【配置Hifive1-revB】设备管理器中不识别端口,Can not connect to J-Link via USB的解决办法
Xintang nuc980 set DHCP or static IP
Mongodb source code deployment and configuration
Brush questions - luogu-p1089 Jinjin savings plan
Detailed explanation of how R language converts large Excel files into DTA format
Turn off automatic update when brew executes commands
leetcode202---快乐数
CSV文本文件导入excel的四种方法
Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]
Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component
Interpretation of featdepth self-monitoring model for monocular depth estimation (Part I) -- paper understanding and core source code analysis
Brush questions - Luogu -p1075 prime factor decomposition