当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
随机推荐
abs()、fabs() 和 labs() 的区别
tRNA修饰2-甲基胞嘧啶(m2C)|tRNA修饰m2G (N2-methylguanosine)
leetcode 072. Finding Square Roots
调用EasyCVR云台控制接口时,因网络延迟导致云台操作异常该如何解决?
【leetcode】剑指 Offer II 007. 数组中和为 0 的三个数(双指针)
In-depth understanding of JVM-memory structure
使用 ReportLab 绘制 PDF
Li Mu hands-on learning deep learning V2-BERT fine-tuning and code implementation
async 和 await 原来这么简单
Kubernetes资源编排系列之三: Kustomize篇 作者 艄公(杨京华) 雪尧(郭耀星)
leetcode 448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字(简单)
YARN功能介绍、交互流程及调度策略
转运RNA(tRNA)甲基化修饰7-甲基胞嘧啶(m7C)|tRNA-m7G
一种能有效缓解环境噪声对音频质量干扰的方案
leetcode 1837. The sum of the digits in the K-base representation
leetcode 16.01. 交换数字(不使用临时变量交换2个数的值)
头条服务端一面经典10道面试题解析
李沐动手学深度学习V2-自然语言推断与数据集SNLI和代码实现
Edge box + time series database, technology selection behind Midea's digital platform iBuilding
【HiFlow】经常忘记签到怎么办?使用腾讯云场景连接器每天提醒你。








![【微信小程序2】事件传参与数据同步[03]](/img/d9/73004e6edf800c583231a94dfbd878.png)
