当前位置:网站首页>Leetcode question brushing: String 02 (reverse string II)
Leetcode question brushing: String 02 (reverse string II)
2022-06-26 20:50:00 【Taotao can't learn English】
541. Reverse string II
Given a string s And an integer k, You need to check every 2k Before characters k Characters to reverse .
If the remaining characters are less than k individual , Reverse all remaining characters .
If the remaining characters are less than 2k But greater than or equal to k individual , Before reversal k Characters , The rest of the characters remain the same .
Example :
Input : s = “abcdefg”, k = 2
Output : “bacdfeg”
It's every 2*k One for a group ( That's important !), In the complete group , front k A reversal , after k No change . The rest is less than half , Namely k, Then reverse all and add , If more than half , Then that k Just reverse it .
There are two special cases , There is less than one group . I took it out alone .
package com.programmercarl.string;
/** * @ClassName ReverseStr * @Descriotion TODO * @Author nitaotao * @Date 2022/6/23 17:45 * @Version 1.0 * https://leetcode.cn/problems/reverse-string-ii/ * 541. Reverse string II **/
public class ReverseStr {
public static void main(String[] args) {
System.out.println(reverseStr("abcdefg", 3));
}
public static String reverseStr(String s, int k) {
// To put it bluntly, every [0,k-1] reverse [k,2k-1] unchanged
// rotary time Time 45
int time = s.length() / (2 * k);
String result = "";
if (s.length() < k) {
result += reverseString(s.substring(0, s.length()).toCharArray());
return result;
}
if (s.length() <= 2 * k) {
result += reverseString(s.substring(0, k).toCharArray());
result += s.substring(k);
return result;
}
int index = 0;
while (index < time) {
result += reverseString(s.substring(index * 2 * k, index * 2 * k + k).toCharArray());
result += s.substring(index * 2 * k + k, index * 2 * k + 2 * k);
index++;
}
// More than
// If the remaining characters are less than k individual , Reverse all remaining characters .
System.out.println(result);
if (s.length() - time * 2 * k < k) {
result += reverseString(s.substring(time * 2 * k).toCharArray());
} else {
// If the remaining characters are less than 2k But greater than or equal to k individual , Before reversal k Characters , The rest of the characters remain the same .
result += reverseString(s.substring(time * 2 * k, time * 2 * k + k).toCharArray());
result += s.substring(time * 2 * k + k);
}
return result;
}
public static String reverseString(char[] s) {
// Double finger needling
int left = 0;
int right = s.length - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
return String.valueOf(s);
}
}

边栏推荐
- Feitian +cipu body brings more imagination to the metauniverse
- Comment installer la base de données MySQL 8.0 sous Windows? (tutoriel graphique)
- Two methods of QT to realize timer
- Unity - URP get camera stack
- 超分之VRT
- 清华大学就光刻机发声,ASML立马加紧向中国出口光刻机
- C语言 文件光标 fseek
- 【最详细】最新最全Redis面试大全(42道)
- Establish a connection with MySQL
- 515. 在每个树行中找最大值
猜你喜欢

Uni app uses canvas to draw QR code

Arduino UNO + DS1302利用31字节静态RAM存储数据并串口打印

Three basic backup methods of mongodb

Six necessary threat tracking tools for threat hunters
![[serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)](/img/a8/622cddae2ac8c383979ed51d36bca9.jpg)
[serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)

Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles

分布式ID生成系统

西瓜书重温(七): 贝叶斯分类器(手推+代码demo)

The relationship between the development of cloud computing technology and chip processor

Dynamic parameter association using postman
随机推荐
Separate save file for debug symbols after strip
Tiktok practice ~ sharing module ~ generate short video QR code
Stop being a giant baby
MySQL - table creation and management
Daily basic use of alicloud personal image warehouse
【贝叶斯分类3】半朴素贝叶斯分类器
Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles
Swagger: how to generate beautiful static document description pages
两个文件 合并为第三个文件 。
515. 在每个树行中找最大值
Disruptor local thread queue_ Use transprocessor processor and workpool to compare consumption - Notes on inter thread communication 005
Three basic backup methods of mongodb
Solve com mysql. jdbc. exceptions. jdbc4.MySQLNonTransientConnectionException: Could not create connection
Super VRT
Arduino UNO + DS1302利用31字节静态RAM存储数据并串口打印
【贝叶斯分类4】贝叶斯网
飞天+CIPU体为元宇宙带来更大想象空间
MongoDB实现创建删除数据库、创建删除表(集合)、数据增删改查
leetcode刷题:字符串03(剑指 Offer 05. 替换空格)
QT两种方法实现定时器