当前位置:网站首页>Leetcode question brushing: String 05 (Sword finger offer 58 - ii. left rotation string)
Leetcode question brushing: String 05 (Sword finger offer 58 - ii. left rotation string)
2022-06-26 20:48:00 【Taotao can't learn English】
subject : The finger of the sword Offer58-II. Left rotation string
The left rotation operation of string is to transfer several characters in front of string to the end of string . Please define a function to implement the left rotation operation of string . such as , Input string "abcdefg" And number 2, This function will return the result of rotating two bits to the left "cdefgab".
Example 1:
Input : s = “abcdefg”, k = 2
Output : “cdefgab”
Example 2:
Input : s = “lrloseumgh”, k = 6
Output : “umghlrlose”
Limit :
1 <= k < s.length <= 10000
As soon as this question is brought up , It takes brains ?
public String reverseLeftWords1(String s, int n) {
// Intercept the first left of the current string n position
String head = s.substring(0, n);
return s.substring(n) + head;
}

Let's look at the solution
Do it again according to the idea of problem solving .
public String reverseLeftWords(String s, int n) {
StringBuilder result = new StringBuilder(s);
// Reverse the first half
reverseStr(result, 0, n);
// Reverse the second half
reverseStr(result, n, s.length());
// Reverse entire segment
reverseStr(result, 0, s.length());
return String.valueOf(result);
}
/** * Inverts the string in the specified interval * * @param s * @param start * @param end * @return */
public static StringBuilder reverseStr(StringBuilder s, int start, int end) {
while (start < end) {
char temp = s.charAt(start);
s.setCharAt(start, s.charAt(end - 1));
s.setCharAt(end-1, temp);
start++;
end--;
}
return s;
}

There is one saying. , I can't think of doing this without looking at the solution .
边栏推荐
- 西瓜书重温(七): 贝叶斯分类器(手推+代码demo)
- Is there any risk in opening a mobile stock registration account? Is it safe?
- 案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:- 打开系统有欢迎界面,并显示可选择的选项- 选项1:记录比赛得分- 选项2:查看往届
- C primer plus learning notes - 3. Character IO (input / output)
- MySQL中存储过程的详细详解
- Keep alive cache component in Vue
- When are global variables initialized before entering the main function?
- Gamefi active users, transaction volume, financing amount and new projects continue to decline. Can axie and stepn get rid of the death spiral? Where is the chain tour?
- Mr. Sun's version of JDBC (21:34:25, June 12, 2022)
- 515. 在每个树行中找最大值
猜你喜欢

GEE:计算image区域内像素最大最小值
![[recommended collection] these 8 common missing value filling skills must be mastered](/img/ab/353f74ad73ca592a3f97ea478922d9.png)
[recommended collection] these 8 common missing value filling skills must be mastered

Three basic backup methods of mongodb

leetcode刷题:字符串06(实现 strStr())

Database SQL statement writing

【连载】说透运维监控系统01-监控系统概述

Tiktok practice ~ search page ~ scan QR code

Unity——Mathf. Similarities and differences between atan and atan2
![[serial] shuotou O & M monitoring system 01 overview of monitoring system](/img/b2/bc75a4d0c8d98056d93ba99b3e6193.png)
[serial] shuotou O & M monitoring system 01 overview of monitoring system
MySQL中存储过程的详细详解
随机推荐
[serial] shuotou O & M monitoring system 01 overview of monitoring system
Garbage collection mechanism of browser
C: Reverse linked list
Sword finger offer II 091 Paint the house
【贝叶斯分类4】贝叶斯网
Browser event loop
【山东大学】考研初试复试资料分享
Developer survey: rust/postgresql is the most popular, and PHP salary is low
Is it safe to open an account for CICC Wealth Online?
Distributed ID generation system
ImageView, glide load long picture (glide load picture)
好物推薦:移動端開發安全工具
阿里云个人镜像仓库日常基本使用
Daily basic use of alicloud personal image warehouse
回溯思路详解
How to install mysql8.0 database under Windows system? (Graphic tutorial)
Muke 8. Service fault tolerance Sentinel
c语言简单的登录
c语言99乘法表
SentinelResource注解詳解
