当前位置:网站首页>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;
}
};
边栏推荐
- 【飞控开发高级教程6】疯壳·开源编队无人机-AI语音控制
- Benchmarking Lane-changing Decision-making for Deep Reinforcement Learning
- 622 设计循环队列——Leetcode天天刷【循环队列,数组模拟,双指针】(2022.8.2)
- 消除对特权账户的依赖使用Kaniko构建镜像
- 剑指 Offer II 044. 二叉树每层的最大值-dfs法
- 「学习笔记」高斯消元
- 倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即
- ES6解构赋值--数组解构及对象解构
- 149. The largest number on a straight line, and check the set
- RNA-ATTO 390|RNA-ATTO 425|RNA-ATTO 465|RNA-ATTO 488|RNA-ATTO 495|RNA-ATTO 520近红外荧光染料标记核糖核酸RNA
猜你喜欢
随机推荐
【HiFlow】经常忘记签到怎么办?使用腾讯云场景连接器每天提醒你。
ES6--剩余参数
JMeter笔记5 |Badboy使用和录制
glusterfs 搭建使用
模板字符串概述
【leetcode】剑指 Offer II 008. 和大于等于 target 的最短子数组(滑动窗口,双指针)
MapReduce介绍及执行过程
李沐动手学深度学习V2-自然语言推断与数据集SNLI和代码实现
【微信小程序2】事件传参与数据同步[03]
leetcode 899. 有序队列
tRNA-m5C转运RNA(tRNA)修饰5-甲基胞嘧啶(m5C)|tRNA修饰m1Am2A (2-methyladenosine)
1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)
双线性插值公式推导及Matlab实现
Auto.js脚本程序打包
2022 CCF中国开源大会会议通知(第三轮)
ES6 deconstruction assignment - array object deconstruction and deconstruction
Anaconda 虚拟环境迁移
Use ControlTemplate or Style from resource file in WPF .cs and find the control
leetcode 1837. The sum of the digits in the K-base representation
一种能有效缓解环境噪声对音频质量干扰的方案