当前位置:网站首页>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;
}
};边栏推荐
- LeetCode 454. Add four numbers II
- 深入理解 grpc part1
- 华为HMS Core携手超图为三维GIS注入新动能
- epoll介绍
- Seckill system 03 - redis cache and distributed lock
- Dataset partitioning script for classification tasks
- About keil compiler, "file has been changed outside the editor, reload?" Solutions for
- Kernel synchronization mechanism
- 自定义 grpc 插件
- Sum of factor numbers of interval product -- prefix sum idea + fixed one shift two
猜你喜欢

Comment Cao définit la décimale de dimension

CPU 上下文切换的机制和类型 (CPU Context Switch)

NOV Schedule for . Net to display and organize appointments and recurring events

谈思生物直播—GENOVIS张洪妍抗体特异性酶切技术助力抗体药物结构表征

如何看懂开发的查询语句

Harbor webhook from principle to construction

博途V15添加GSD文件

Neo4j Chinese developer monthly - issue 202206

Abbirb120 industrial robot mechanical zero position

【单片机】【数码管】数码管显示
随机推荐
C # dependency injection (straight to the point) will be explained as soon as you see the series
MQ prevent message loss and repeated consumption
redis中value/list
CPI tutorial - asynchronous interface creation and use
Compile and debug net6 source code
Chen Gong: Micro service, is it still so pure?
Emotion analysis based on IMDB comment data set
How does Nike dominate the list all the year round? Here comes the answer to the latest financial report
uniapp 使用 uni-upgrade-center
区间乘积的因子数之和——前缀和思想+定一移二
博途V15添加GSD文件
The operation process of using sugar to make a large data visualization screen
I'm in Zhongshan. Where is a better place to open an account? Is it actually safe to open an account online?
redis常识
Istio, ebpf and rsocket Broker: in depth study of service grid
JS date format conversion method
自组织是管理者和成员的双向奔赴
Leetcode force buckle (Sword finger offer 31-35) 31 Stack push pop-up sequence 32i II. 3. Print binary tree from top to bottom 33 Post order traversal sequence 34 of binary search tree The path with a
C summary of knowledge points 3
消息队列之监控退款任务批处理过程