当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
随机推荐
ES6-箭头函数
(十六)51单片机——红外遥控
JWT详解
若依集成browscap读取浏览器用户代理
【HiFlow】经常忘记签到怎么办?使用腾讯云场景连接器每天提醒你。
ES6 deconstruction assignment - array object deconstruction and deconstruction
数学之美 第六章——信息的度量和作用
149. 直线上最多的点数-并查集做法
node版本切换工具NVM以及npm源管理器nrm
使用 ReportLab 绘制 PDF
算法--交错字符串(Kotlin)
YARN功能介绍、交互流程及调度策略
宁德时代2号人物黄世霖辞任副董事长:身价1370亿
【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)
LeetCode 899. 有序队列
tensorflow-gpu2.4.1安装配置详细步骤
汉源高科8光口12电口交换机千兆8光8电12电16电网管型工业以太网交换机
双线性插值公式推导及Matlab实现
Benchmarking Lane-changing Decision-making for Deep Reinforcement Learning
揭秘5名运维如何轻松管理数亿级流量系统









