当前位置:网站首页>Leetcode (Sword finger offer) - 58 - ii Rotate string left
Leetcode (Sword finger offer) - 58 - ii Rotate string left
2022-07-01 12:07:00 【Programmer Mu code】
Topic link : Click to open the link
The main idea of the topic : A little .
Their thinking : A little .
Related enterprises
- Bytes to beat
AC Code
- Java
// Solution (1)
class Solution {
public String reverseLeftWords(String s, int n) {
return s.substring(n) + s.substring(0, n);
}
}
// Solution (2)
class Solution {
public String reverseLeftWords(String s, int n) {
StringBuilder res = new StringBuilder();
for(int i = n; i < s.length(); i++)
res.append(s.charAt(i));
for(int i = 0; i < n; i++)
res.append(s.charAt(i));
return res.toString();
}
}
// Solution (3)
class Solution {
public String reverseLeftWords(String s, int n) {
StringBuilder res = new StringBuilder();
for(int i = n; i < n + s.length(); i++)
res.append(s.charAt(i % s.length()));
return res.toString();
}
}- C++
// Solution (1)
class Solution {
public:
string reverseLeftWords(string s, int n) {
return s.substr(n, s.size()) + s.substr(0, n);
}
};
// Solution (2)
class Solution {
public:
string reverseLeftWords(string s, int n) {
reverseString(s, 0, n - 1);
reverseString(s, n, s.size() - 1);
reverseString(s, 0, s.size() - 1);
return s;
}
private:
void reverseString(string& s, int i, int j) {
while(i < j) swap(s[i++], s[j--]);
}
};
// Solution (3)
class Solution {
public:
string reverseLeftWords(string s, int n) {
reverse(s.begin(), s.begin() + n);
reverse(s.begin() + n, s.end());
reverse(s.begin(), s.end());
return s;
}
};边栏推荐
- 91. (chapitre Cesium) simulation de lancement de fusées cesium
- JS date format conversion method
- I'm in Zhongshan. Where is a better place to open an account? Is it actually safe to open an account online?
- 241. 为运算表达式设计优先级 : DFS 运用题
- The specified service is marked for deletion
- Value/list in redis
- 力扣首页简介动画
- Force button homepage introduction animation
- LeetCode力扣(剑指offer 31-35)31. 栈的压入弹出序列32I.II.III.从上到下打印二叉树33. 二叉搜索树的后序遍历序列34. 二叉树中和为某一值的路径35. 复杂链表的复制
- GID: open vision proposes a comprehensive detection model knowledge distillation | CVPR 2021
猜你喜欢

CAD如何設置標注小數比特

Acly and metabolic diseases
![[Yunju entrepreneurial foundation notes] Chapter VII Entrepreneurial Resource test 1](/img/be/1194125442aaa2d7cc20b6a4a6762a.jpg)
[Yunju entrepreneurial foundation notes] Chapter VII Entrepreneurial Resource test 1

ACLY与代谢性疾病

【单片机】【数码管】数码管显示

CAD如何设置标注小数位

【20211129】Jupyter Notebook远程服务器配置

Exploration and practice of inress in kubernetes
![[Maui] add click events for label, image and other controls](/img/d6/7ac9632681c970ed99c9e4d3934ddc.jpg)
[Maui] add click events for label, image and other controls

Abbirb120 industrial robot mechanical zero position
随机推荐
Dlhsoft Kanban, Kanban component of WPF
Want to ask, is there a discount for opening a securities account? Is it safe to open a mobile account?
Redis common sense
[Yunju entrepreneurial foundation notes] Chapter VII Entrepreneurial Resource test 1
Value/string in redis
91. (cesium chapter) cesium rocket launch simulation
Le semester manquant
ABBIRB120工业机器人机械零点位置
Neo4j Chinese developer monthly - issue 202206
Comment Cao définit la décimale de dimension
博途V15添加GSD文件
Value/set in redis
sshd_ Discussion on permitrotlogin in config
指纹浏览器工作原理、使用场景以及重要性简单讲解
我在中山,到哪里开户比较好?实际上网上开户安全么?
easyexcel的使用
Consolidate -c operator
241. 为运算表达式设计优先级 : DFS 运用题
深入理解 grpc part1
Exploration and practice of inress in kubernetes