当前位置:网站首页>剑指Offer第二版:字符串(简单)
剑指Offer第二版:字符串(简单)
2022-07-23 21:17:00 【_索伦】
剑指 Offer 05. 替换空格
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
原题链接:剑指 Offer 05. 替换空格
示例:
输入:s = “We are happy.”
输出:“We%20are%20happy.”
题解
class Solution {
public:
string replaceSpace(string s) {
if (s.empty()) {
return s;
}
string ans;
for (char c : s) {
if (c == ' ') {
ans.push_back('%');
ans.push_back('2');
ans.push_back('0');
}
else {
ans.push_back(c);
}
}
return ans;
}
};
剑指 Offer 58 - II. 左旋转字符串
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
题解
局部反转+整体反转
时间复杂度O(N)
空间复杂度O(1)
class Solution {
public:
string reverseLeftWords(string s, int n) {
if (s.empty()) return s;
reverse(s.begin(), s.begin() + n);
reverse(s.begin() + n, s.end());
reverse(s.begin(), s.end());
return s;
}
};
字符串遍历
时间复杂度 O(N)
空间复杂度 O(N)
class Solution {
public:
string moveLeft(string s, int left) {
string ans = "";
for (int i = 0; i < s.length(); ++i) {
ans += s[(i + left) % s.length()];
}
return ans;
}
string reverseLeftWords(string s, int n) {
if (s.empty()) return s;
int left = n % s.length();
if (left == 0) return s;
else return moveLeft(s, left);
}
};
边栏推荐
- High numbers | calculation of double integral 3 | high numbers | handwritten notes
- Ssm+mysql to realize snack mall system (e-commerce shopping)
- Major optimization of openim - Message loading on demand, consistent cache, uniapp Publishing
- Chapter 2 回归
- High numbers | calculation of double integral 4 | high numbers | handwritten notes
- 221. 最大正方形 ●● & 1277. 统计全为 1 的正方形子矩阵 ●●
- Comment présenter votre expérience de projet lors d'une entrevue
- Install under win7-vs2012 Net framework work
- (Note)优化器Adam的学习率设置
- Be a professional software craftsman
猜你喜欢

【攻防世界WEB】难度四星12分进阶题:FlatScience

【isprint函数判断字符是否可输出】

【攻防世界WEB】难度四星12分进阶题:Confusion1

Basic knowledge of mobile phone testing

LeetCode_376_摆动序列
![[100 cases of scratch drawing] Figure 46-scratch drawing flowers children's programming scratch programming drawing case tutorial grade examination competition drawing training case](/img/44/832686f3ee198772794cbd53e7d5a5.png)
[100 cases of scratch drawing] Figure 46-scratch drawing flowers children's programming scratch programming drawing case tutorial grade examination competition drawing training case

Problems and abuse of protocol buffers

VLAN综合实验

At 12 o'clock on July 23, 2022, the deviation from the top of the line of love life hour appeared, maintaining a downward trend and waiting for the rebound signal.

第三届SLAM技术论坛-吴毅红教授
随机推荐
LU_ Asr01 voice module usage
Stm32c8t6 driving lidar actual combat (II)
【Scratch画图100例】图46-scratch绘制花朵 少儿编程 scratch编程画图案例教程 考级比赛画图集训案例
flink原理及开发总结(详细)
Synchro esp32c3 Hardware Configuration Information serial port Print Output
vite3学习记录
It's good to change jobs for a while, and it's good to change jobs all the time?
TCP half connection queue and full connection queue (the most complete in History)
Jetson nano烧录踩坑记(一定可以解决你的问题)
Chapter 3 business function development (creating clues)
告诉我十大证券公司?请问网上开户安全么?
手机测试相关基础知识
Car rental vehicle management system based on jsp+ssm+mysql car rental
Chapter1 data cleaning
当我们在谈论陈春花和华为时,我们到底在讨论什么?
SQLite database
Failed to introspect Class FeignClientFactoryBean 异常排查
Unity solves that animation is not available: the animationclip 'xxx' used by the animation component 'xxx' must be marked as legacy
When we talk about Chen Chunhua and Huawei, what are we talking about?
Major optimization of openim - Message loading on demand, consistent cache, uniapp Publishing