当前位置:网站首页>~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();
}
}
边栏推荐
- 「数字安全」警惕 NFT的七大骗局
- 飞沃科技IPO过会:年营收11.3亿 湖南文旅与沅澧投资是股东
- Pytorch uses tensorboard to realize visual summary
- Brush questions - Luogu -p1059 clear random number
- [configure hifive1 revb] the device manager does not recognize the port, and can not connect to j-link via USB
- IM系统-消息流化一些常见问题
- Feiwo technology IPO meeting: annual revenue of 1.13 billion Hunan Cultural Tourism and Yuanli investment are shareholders
- 手里有点钱可以投资哪些理财产品?
- Gartner 2022 top technology trend: Super automation
- 【力扣】645.错误的集合
猜你喜欢

Brush questions - Luogu -p1146 coin flip

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

Brush questions - Luogu -p1085 unhappy Jinjin

Leetcode1 -- sum of two numbers

swiper 一侧或两侧露出一小部分

leetcode--四数相加II

Mxnet implementation of densenet (dense connection network)

Brush questions - Luogu -p1047 trees outside the school gate

数字孪生 - 认知篇

Hyperautomation for the enhancement of automation in industries
随机推荐
Day1:三种语言暴刷牛客130题
CSV文本文件导入excel的四种方法
Brush questions - Luogu -p1152 happy jump
MySQL and Navicat installation and stepping on pits
Okaleido ecological core equity Oka, all in fusion mining mode
maya建模练习
swiper 一侧或两侧露出一小部分
NoSQL,关系型数据库,行列数据库对比、类比
Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]
R语言如何将大型Excel文件转为dta格式详解
NUC980 设置SSH Xshell连接
Feiwo technology IPO meeting: annual revenue of 1.13 billion Hunan Cultural Tourism and Yuanli investment are shareholders
Basic theory of monocular depth estimation and paper learning summary
Stay on Alibaba cloud mqtt IOT platform
Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component
在线问题反馈模块实战(十三):实现多参数分页查询列表
Leetcode202 --- Happy number
Business data analysis of CDA level1 knowledge point summary
Oka pass rights and interests analysis is the best choice to participate in okaleido ecological construction
word设置粘贴仅保留文本