当前位置:网站首页>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 .
边栏推荐
- IDEA 报错:Process terminated【已解决】
- MySQL stored procedure
- Dynamic parameter association using postman
- Tiktok practice ~ sharing module ~ generate short video QR code
- [most detailed] the latest and complete redis interview (70)
- c语言99乘法表
- vue中缓存组件keep-alive
- 回首望月
- The source code that everyone can understand (I) the overall architecture of ahooks
- [Bayesian classification 4] Bayesian network
猜你喜欢

c语言99乘法表

GameFi 活跃用户、交易量、融资额、新项目持续性下滑,Axie、StepN 能摆脱死亡螺旋吗?链游路在何方?

【贝叶斯分类3】半朴素贝叶斯分类器

Unity——Mathf. Similarities and differences between atan and atan2

leetcode刷题:字符串01(反转字符串)

MySQL - database creation and management

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

飞天+CIPU体为元宇宙带来更大想象空间

GEE:计算image区域内像素最大最小值
![[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
随机推荐
【贝叶斯分类3】半朴素贝叶斯分类器
MySQL stored procedure
网上开户万一免五到底安不安全?
2022/02/14 line generation
Unity——Mathf. Similarities and differences between atan and atan2
【贝叶斯分类2】朴素贝叶斯分类器
论数据库的传统与未来之争之溯源溯本----AWS系列专栏
Detailed explanation of shutter textfield
The relationship between the development of cloud computing technology and chip processor
案例描述:比赛分数管理系统,需要统计历届冠军所得比赛得分,并记录到文件中,其中系统有如下需求:- 打开系统有欢迎界面,并显示可选择的选项- 选项1:记录比赛得分- 选项2:查看往届
Super VRT
Is it safe to open an online account in case of five-year exemption?
Idea error: process terminated
Is it safe to open an account for CICC Wealth Online?
开户可以在网上开么?能安全吗?
西瓜书重温(七): 贝叶斯分类器(手推+代码demo)
Looking back at the moon
剑指 Offer II 091. 粉刷房子
QT两种方法实现定时器
mysql存储过程
