当前位置:网站首页>401-字符串(344. 反转字符串、541. 反转字符串II、题目:剑指Offer 05.替换空格、151. 颠倒字符串中的单词)
401-字符串(344. 反转字符串、541. 反转字符串II、题目:剑指Offer 05.替换空格、151. 颠倒字符串中的单词)
2022-06-22 05:35:00 【liufeng2023】
344. 反转字符串

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. 反转字符串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;
}
};

题目:剑指Offer 05.替换空格

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. 颠倒字符串中的单词
边栏推荐
- Why is "CPS alliance marketing" the most cost-effective promotion method?
- Sourcetree reported an error SSH failure
- Sogou input method cannot output Chinese
- Vulkan 预旋转处理设备方向
- Development planning and investment strategy analysis report of global and Chinese microwave ablation industry during the 14th Five Year Plan period 2022-2027
- Assertion based validation
- \[\e]0; \[email protected]\h: \w\a\]\[\033[01;32m\]\[email protected]\h\[\033[
- 信号输出库
- Write optimized DSP code for cortex-m4
- Research Report on demand and investment opportunities in key areas of global and Chinese strontium tungstate industry 2022-2027
猜你喜欢

记录在处理SIF数据中,遇到的一些问题及解决过程

Someone always asks me: how to play independent station? Three cases, you will understand after reading

为Cortex-M4编写优化的DSP代码

Reptile initial and project

MinGW下载安装

An unordered array of N integers. Find the first number after each element that is larger than it. The time complexity is O (n)

Record some problems and solutions encountered in processing SIF data

Beifeng helps Nanchang Emergency Management Bureau to build a public-private integrated emergency communication guarantee network

以太网UDP帧发包设计

Working method: 3C scheme design method
随机推荐
Dos Bat 语法记录一
AUTOSAR从入门到精通100讲(150)-SOA架构及应用
记录在处理SIF数据中,遇到的一些问题及解决过程
Go语言使用zap日志库
vscode 远程连接错误:Server status check failed - waiting and retrying
Market survey and future production and marketing demand analysis report of China's zinc oxide nanoparticle industry 2022-2027
Grabcut analysis
MinGW download and installation
Tensorflow 2.x(keras)源码详解之第十四章:keras中的回调及自定义回调
Using SystemVerilog to describe a state machine
printf becomes puts
向量空间视角看高数(1)——系列简介
An unordered array of N integers. Find the first number after each element that is larger than it. The time complexity is O (n)
Report on global and Chinese direct coupled actuator market demand and future development planning recommendations 2022-2027
Trigger
A simple method to implement deep cloning and encapsulation of objects JS
Academic circle of cattle II (daily question 49 in spring)
QEMU ARM interrupt system architecture
为Cortex-M4编写优化的DSP代码
爬虫初始及项目