当前位置:网站首页>leetcode 剑指 Offer 58 - II. 左旋转字符串
leetcode 剑指 Offer 58 - II. 左旋转字符串
2022-08-03 20:06:00 【会编程的露娜】
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
示例 1:
输入: s = “abcdefg”, k = 2
输出: “cdefgab”
示例 2:
输入: s = “lrloseumgh”, k = 6
输出: “umghlrlose”
限制:
1 <= k < s.length <= 10000
class Solution {
public:
string reverseLeftWords(string s, int n) {
string res=s; //保证res和s的元素个数相同
int m=s.length();
for(int i=0;i<m;++i)
res[i]=s[(i+n)%m]; //在往后加的过程中可能会超出字符串的长度,所以要去模再回到原来的位置
return res;
}
};
class Solution {
public:
string reverseLeftWords(string s, int n) {
string s1="",s2="";
for(int i=1;i<=n;++i)
s1+=s[i-1];
for(int i=n+1;i<=s.length();++i)
s2+=s[i-1];
s2+=s1;
return s2;
}
};
边栏推荐
- ECCV 2022 Oral | 满分论文!视频实例分割新SOTA: IDOL
- 化算力为战力:宁夏中卫的数字化转型启示录
- 虚拟机vmware设置桥接模式上网
- 若依集成easyexcel实现excel表格增强
- 力扣206-反转链表——链表
- Edge box + time series database, technology selection behind Midea's digital platform iBuilding
- PHP according to the longitude and latitude calculated distance two points
- 盘点在线帮助中心对企业能够起到的作用
- ARMuseum
- 开源生态研究与实践| ChinaOSC
猜你喜欢
Detailed demonstration pytorch framework implementations old photo repair (GPU)
In-depth understanding of JVM-memory structure
tRNA甲基化偶联3-甲基胞嘧啶(m3C)|tRNA-m3C (3-methylcy- tidine)
codeforces:C. Maximum Subrectangle【前缀和 + 贪心 + 最小子数组和】
从文本匹配到语义相关——新闻相似度计算的一般思路
转运RNA(tRNA)甲基化修饰7-甲基胞嘧啶(m7C)|tRNA-m7G
Edge box + time series database, technology selection behind Midea's digital platform iBuilding
(十六)51单片机——红外遥控
单调栈及其应用
那些年我写过的语言
随机推荐
若依集成browscap读取浏览器用户代理
高位套牢机构,用友网络的信任危机是如何产生的?
刷题错题录1-隐式转换与精度丢失
嵌入式分享合集27
极验深知v2分析
怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)
JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
tRNA修饰2-甲基胞嘧啶(m2C)|tRNA修饰m2G (N2-methylguanosine)
ERROR: You don‘t have the SNMP perl module installed.
利用 rpush 和 blpop 实现 Redis 消息队列
glusterfs 搭建使用
Auto.js脚本程序打包
RNA核糖核酸修饰Alexa 568/[email protected] 594/[email prote
PHP according to the longitude and latitude calculated distance two points
不要再用if-else
简易电子琴设计(c语言)
WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
消除对特权账户的依赖使用Kaniko构建镜像
Auto.js实现朋友圈自动点赞
后台图库上传功能