当前位置:网站首页>leetcode refers to Offer 58 - II. Left Rotate String
leetcode refers to Offer 58 - II. Left Rotate String
2022-08-03 20:12:00 【Luna programming】
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部.请定义一个函数实现字符串左旋转操作的功能.比如,输入字符串"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]; //In the process of adding later, the length of the string may be exceeded,So go to the mold and go back to the original position
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;
}
};
边栏推荐
猜你喜欢
随机推荐
JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
云服务器如何安全使用本地的AD/LDAP?
Lecture topics and guest blockbuster, TDengine developers conference to promote data technology "broken"
调用EasyCVR云台控制接口时,因网络延迟导致云台操作异常该如何解决?
多模态 参考资料汇总
In-depth understanding of JVM-memory structure
node版本切换工具NVM以及npm源管理器nrm
CLIP论文解读
leetcode 剑指 Offer 58 - II. 左旋转字符串
C51 存储类型与存储模式
自定义form表单验证
tRNA甲基化偶联3-甲基胞嘧啶(m3C)|tRNA-m3C (3-methylcy- tidine)
微导纳米IPO过会:年营收4.28亿 君联与高瓴是股东
ES6 deconstruction assignment - array object deconstruction and deconstruction
leetcode 剑指 Offer 15. 二进制中1的个数
2022 CCF中国开源大会会议通知(第三轮)
NNLM、RNNLM等语言模型 实现 下一单词预测(next-word prediction)
力扣203-移除链表元素——链表
async 和 await 原来这么简单
谁的孙子最多II