当前位置:网站首页>【剑指 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;
}
};
边栏推荐
- ThreeJS 第二篇:顶点概念、几何体结构
- PHP CI(CodeIgniter)log级别设置
- [redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
- 相同切入点的抽取
- Register in PHP_ Globals parameter settings
- [combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
- 【LeetCode】94. Middle order traversal of binary tree
- 无心剑中译泰戈尔《漂鸟集(1~10)》
- 爱可可AI前沿推介(7.3)
- [list to map] collectors Tomap syntax sharing (case practice)
猜你喜欢

Aike AI frontier promotion (7.3)

消息队列消息丢失和消息重复发送的处理策略

TCP拥塞控制详解 | 3. 设计空间

Cocos Creator 2.x 自动打包(构建 + 编译)

TCP擁塞控制詳解 | 3. 設計空間

How to use AAB to APK and APK to AAB of Google play apps on the shelves

于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (III)

Visual SLAM algorithms: a survey from 2010 to 2016

Cocos Creator 2. X automatic packaging (build + compile)
随机推荐
The accept attribute of the El upload upload component restricts the file type (detailed explanation of the case)
Golang decorator mode and its use in NSQ
(补)双指针专题
Svn usage specification
2022爱分析· 国央企数字化厂商全景报告
Is it safe to open a stock account by mobile registration? Does it need money to open an account
Mysql 将逗号隔开的属性字段数据由列转行
记一次jar包冲突解决过程
How to initialize views when loading through storyboards- How is view initialized when loaded via a storyboard?
Is it safe to open an account with flush?
Remote file contains actual operation
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
Record windows10 installation tensorflow-gpu2.4.0
Custom plug-in construction and use of QT plug-in
Data driving of appium framework for mobile terminal automated testing
探索Cassandra的去中心化分布式架构
Develop team OKR in the way of "crowdfunding"
Myopia: take off or match glasses? These problems must be understood clearly first
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
Mysql 单表字段重复数据取最新一条sql语句