当前位置:网站首页>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;
}
};边栏推荐
- Force button homepage introduction animation
- MQ prevent message loss and repeated consumption
- Impressive bug summary (continuously updated)
- 91.(cesium篇)cesium火箭发射模拟
- 91.(cesium篇)cesium火箭發射模擬
- Redis的攻击手法
- 构建外部模块(Building External Modules)
- Learning summary on June 30, 2022
- MQ-防止消息丢失及重复消费
- Summary of JFrame knowledge points 2
猜你喜欢

Joint Time-Frequency and Time Domain Learning for Speech Enhancement

Uniapp uses uni upgrade Center

easyexcel的使用

Self organization is the two-way rush of managers and members

Rural guys earn from more than 2000 a month to hundreds of thousands a year. Most brick movers can walk my way ǃ

About keil compiler, "file has been changed outside the editor, reload?" Solutions for

Redis' attack tactics

Explore the contour detection function findcontours() of OpenCV in detail with practical examples, and thoroughly understand the real role and meaning of each parameter and mode

巩固-C#运算符

Mechanism and type of CPU context switch
随机推荐
Use set_ Handler filters out specific SystemC wrapping & error messages
Machine learning - Data Science Library - day two
Le semester manquant
CPU 上下文切换的机制和类型 (CPU Context Switch)
比特熊直播间一周年,英雄集结令!邀你来合影!
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
leetcode 406. Queue reconstruction by height
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 5
研发效能度量框架解读
C # dependency injection (straight to the point) will be explained as soon as you see the series
The Missing Semester
Technology sharing | MySQL: how about copying half a transaction from the database?
Share the method of how to preview PSD format and PSD file thumbnail plug-in [easy to understand]
GID: open vision proposes a comprehensive detection model knowledge distillation | CVPR 2021
91. (cesium chapter) cesium rocket launch simulation
JS date format conversion method
【datawhale202206】pyTorch推荐系统:精排模型 DeepFM&DIN
91. (chapitre Cesium) simulation de lancement de fusées cesium
redis中value/list
uniapp 使用 uni-upgrade-center