当前位置:网站首页>【剑指 Offer】58 - II. 左旋转字符串
【剑指 Offer】58 - II. 左旋转字符串
2022-07-03 16:29:00 【LuZhouShiLi】
剑指 Offer 58 - II. 左旋转字符串
题目
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
思路
- 反转前N字符串
- 反转n后面的字符串
- 反转整个字符串
代码
class Solution {
public:
string reverseLeftWords(string s, int n) {
// 反转前N字符串
reverse(s.begin(),s.begin() + n);
// 反转n后面的字符串
reverse(s.begin() + n,s.end());
// 反转整个字符串
reverse(s.begin(),s.end());
return s;
}
};
边栏推荐
- 用同花顺炒股开户安全吗?
- PHP二级域名session共享方案
- [proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
- Is it safe to open a stock account by mobile registration? Does it need money to open an account
- Explore Netease's large-scale automated testing solutions see here see here
- Construction practice camp - graduation summary of phase 6
- Explore Cassandra's decentralized distributed architecture
- Develop team OKR in the way of "crowdfunding"
- QT串口ui设计和解决显示中文乱码
- [redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
猜你喜欢
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
线程池执行定时任务
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
From "zero sum game" to "positive sum game", PAAS triggered the third wave of cloud computing
8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
Mixlab编辑团队招募队友啦~~
Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
NSQ源码安装运行过程
Netease UI automation test exploration: airtest+poco
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
随机推荐
架构实战营 - 第 6 期 毕业总结
2022爱分析· 国央企数字化厂商全景报告
[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)
ThreeJS 第二篇:顶点概念、几何体结构
深入理解 SQL 中的 Grouping Sets 语句
SVN使用规范
(补)双指针专题
Mb10m-asemi rectifier bridge mb10m
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
0214-27100 a day with little fluctuation
TCP拥塞控制详解 | 3. 设计空间
pycharm错Error updating package list: connect timed out
AcWing 第58 场周赛
Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
Unreal_ Datatable implements ID self increment and sets rowname
Visual SLAM algorithms: a survey from 2010 to 2016
Initial test of scikit learn Library
[statement] about searching sogk1997 and finding many web crawler results
Cocos Creator 2.x 自动打包(构建 + 编译)
用同花顺炒股开户安全吗?