当前位置:网站首页>401 string (344. reverse string, 541. reverse string II, Title: Sword finger offer 05. replace spaces, 151. reverse words in string)
401 string (344. reverse string, 541. reverse string II, Title: Sword finger offer 05. replace spaces, 151. reverse words in string)
2022-06-22 05:56:00 【liufeng2023】
344. Reverse string

class Solution {
public:
void reverseString(vector<char>& s) {
for (int i = 0, j = s.size() - 1; i < s.size() / 2; i++, j--)
{
s[i] ^= s[j];
s[j] ^= s[i];
s[i] ^= s[j];
}
}
};

541. Reverse string II

class Solution {
public:
string reverseStr(string s, int k) {
for(int i = 0; i < s.size(); i += (2*k))
{
if(i + k <= s.size())
{
reverse(s.begin()+i, s.begin()+i+k);
continue;
}
reverse(s.begin()+i,s.end());
}
return s;
}
};

subject : The finger of the sword Offer 05. Replace blank space

class Solution {
public:
string replaceSpace(string s) {
int old_size = s.size();
int blank_count = 0;
for (int i = 0; i < old_size; i++)
{
if (s[i] == ' ')
{
blank_count++;
}
}
s.resize(old_size + 2 * blank_count);
int new_size = s.size();
for (int i = new_size - 1, j = old_size - 1; i > j ; i--, j--)
{
if (s[j] != ' ')
{
s[i] = s[j];
}
else
{
s[i] = '0';
s[i - 1] = '2';
s[i - 2] = '%';
i -= 2;
}
}
return s;
}
};

151. Invert words in string
边栏推荐
- Implementation of Nacos server source code
- 数据的存储(进阶)
- u盘作为启动盘重装win10系统(无需其他软件)
- Implementation of large file fragment uploading based on webuploader
- MinGW download and installation
- Want to put Facebook ads but don't know where to start? This article takes you to know more about
- MFC TabCtrl 控件修改标签尺寸
- Remove then add string from variable of Makefile
- Facing the bonus period of Google traffic, how do independent station sellers take advantage of the situation to market?
- OPTEE notes
猜你喜欢

自控原理之系统辨识

Reptile initial and project

性能优化 之 3D资产优化及顶点数据管理

402-字符串(题目:剑指Offer58-II.左旋转字符串、 28. 实现 strStr()、459.重复的子字符串)

Clion installation Download

Machine learning note 8: octave for handwritten digit recognition based on Neural Network

MinGW下载安装

Sogou input method cannot output Chinese

Key points of Facebook account "unsealing, anti sealing and maintaining ID" have been collected!

Vscode minimalist installation tutorial
随机推荐
性能优化 之 3D资产优化及顶点数据管理
Someone always asks me: how to play independent station? Three cases, you will understand after reading
Machine learning Note 6: number recognition of multiple classification problems in logistic regression
Working method: 3C scheme design method
Analysis of annual average temperature based on TMP data in cru
北峰助力南昌市应急管理局打造公专融合应急通信保障网
Development prospect and investment potential prediction report of China's rare earth permanent magnet industry during the "14th five year plan" period 2022-2027
EPP (Enhanced Parallel Port 增强型并口)
以太网通信协议
机器学习笔记 六:逻辑回归中的多分类问题之数字识别
Market consumption survey and investment prospect forecast report of China's graphite industry 2022-2027
基于断言的验证
Vue des nombres élevés du point de vue de l'espace vectoriel (1) - - Introduction à la série
AUTOSAR from getting started to mastering 100 lectures (150) -soa architecture and Application
GrabCut分析
Tensorflow 2. Chapter 14: callbacks and custom callbacks in keras
Go language uses JWT
RGB及sRGB与XYZ坐标转换
nacos server 源码运行实现
Remove then add string from variable of Makefile