当前位置:网站首页>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 .
边栏推荐
- C primer plus学习笔记 —— 3、字符的IO(输入/输出)
- 手机股票注册开户有没有什么风险?安全吗?
- Super VRT
- [most detailed] the latest and complete redis interview (70)
- 两个文件 合并为第三个文件 。
- Detailed explanation of retrospective thinking
- 好物推荐:移动端开发安全工具
- 飞天+CIPU体为元宇宙带来更大想象空间
- Developer survey: rust/postgresql is the most popular, and PHP salary is low
- Database SQL statement writing
猜你喜欢
![[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination](/img/f9/68b5b5ce21f4f851439fa061b477c9.jpg)
[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination
![[Bayesian classification 2] naive Bayesian classifier](/img/44/dbff297e536508a7c18b76b21db90a.png)
[Bayesian classification 2] naive Bayesian classifier

GEE:计算image区域内像素最大最小值

动态规划111

【贝叶斯分类2】朴素贝叶斯分类器

c语言99乘法表

The source code that everyone can understand (I) the overall architecture of ahooks

Idea error: process terminated

Good thing recommendation: mobile terminal development security tool

GameFi 活跃用户、交易量、融资额、新项目持续性下滑,Axie、StepN 能摆脱死亡螺旋吗?链游路在何方?
随机推荐
Invocation failed Unexpected end of file from server
手机股票注册开户有没有什么风险?安全吗?
Tiktok practice ~ homepage video ~ pull-down refresh
710. 黑名单中的随机数
MySQL - database creation and management
Case description: the competition score management system needs to count the competition scores obtained by previous champions and record them in the file. The system has the following requirements: -
这些地区考研太疯狂!哪个地区报考人数最多?
The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?
两个文件 合并为第三个文件 。
Muke 11. User authentication and authorization of microservices
Feitian +cipu body brings more imagination to the metauniverse
Is it safe to open an account for CICC Wealth Online?
回溯思路详解
tsconfig. json
What are the specific steps for opening a stock account? Is it safe to open an account online?
710. random numbers in the blacklist
Serial port application program based on gd32
关于Qt数据库开发的一些冷知识
mysql存储过程
Tiktok practice ~ sharing module ~ copy short video link
