当前位置:网站首页>~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();
}
}
边栏推荐
- DNS resolution error during windows unbutu20 lts apt, WGet installation
- 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
- Sunfeng, general manager of Yixun: the company has completed the share reform and is preparing for IPO
- Goldfish rhca memoirs: cl210 managing storage -- managing shared file systems
- 【目录爆破工具】信息收集阶段:robots.txt、御剑、dirsearch、Dirb、Gobuster
- Mongodb源码部署以及配置
- maya建模练习
- [platform IO compile hifive1 revb] * * * [.pio\build\hifive1 revb\src\setupgpio.o] solution to error 1
- einsum(): operands do not broadcast with remapped shapes [original->remapped]: [1, 144, 20, 17]->[1,
- NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
猜你喜欢

AI model risk assessment Part 1: motivation

Leetcode1 -- sum of two numbers

应急科普|收好这份暑期安全指南,让孩子安全过暑假!

Cologne new energy IPO was terminated: the advanced manufacturing and Zhanxin fund to be raised is the shareholder

【力扣】645.错误的集合

Setting of parameter configuration tool for wireless vibrating wire collector

Business data analysis of CDA level1 knowledge point summary

Tm1637 four digit LED display module Arduino driver with second dot

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

Data analysis business core
随机推荐
Use of Bluetooth function of vs wireless vibrating wire acquisition instrument
Mlops column introduction
Brush questions - Luogu -p1075 prime factor decomposition
What financial products can I invest with some money in my hand?
Brush questions - Luogu -p1046 Tao Tao picking apples
"Digital security" alert NFT's seven Scams
Acquisition data transmission mode and online monitoring system of wireless acquisition instrument for vibrating wire sensor of engineering instrument
Oka pass rights and interests analysis is the best choice to participate in okaleido ecological construction
Deep understanding of pytorch distributed parallel processing tool DDP -- starting from bugs in engineering practice
MXNet对DenseNet(稠密连接网络)的实现
[platform IO compile hifive1 revb] * * * [.pio\build\hifive1 revb\src\setupgpio.o] solution to error 1
Applet starts wechat payment
Brush questions - Luogu -p1047 trees outside the school gate
应急科普|收好这份暑期安全指南,让孩子安全过暑假!
Applet sharing function
手把手教学Yolov7的搭建及实践
轻松入门自然语言处理系列 12 隐马尔可夫模型
IDEA设置提交SVN时忽略文件配置
einsum(): operands do not broadcast with remapped shapes [original->remapped]: [1, 144, 20, 17]->[1,
【配置Hifive1-revB】设备管理器中不识别端口,Can not connect to J-Link via USB的解决办法