当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
随机推荐
RNA核糖核酸修饰荧光染料|HiLyte Fluor 488/555/594/647/680/750标记RNA核糖核酸
JMeter笔记5 |Badboy使用和录制
消除对特权账户的依赖使用Kaniko构建镜像
调用EasyCVR接口时视频流请求出现404,并报错SSL Error,是什么原因?
简易电子琴设计(c语言)
ES6简介及let、var、const区别
数学之美 第六章——信息的度量和作用
ES6-箭头函数
PHP according to the longitude and latitude calculated distance two points
leetcode 461. 汉明距离
CLIP论文解读
RNA-ATTO 390|RNA-ATTO 425|RNA-ATTO 465|RNA-ATTO 488|RNA-ATTO 495|RNA-ATTO 520近红外荧光染料标记核糖核酸RNA
那些年我写过的语言
深入理解JVM-内存结构
Solidity智能合约开发 — 4.1-合约创建和函数修饰器
leetcode 2119. 反转两次的数字
xss.haozi练习通关详解
Detailed AST abstract syntax tree
EasyCVR平台海康摄像头语音对讲功能配置的3个注意事项
leetcode 16.01. 交换数字(不使用临时变量交换2个数的值)









