当前位置:网站首页>leetcode刷题:字符串02( 反转字符串II)
leetcode刷题:字符串02( 反转字符串II)
2022-06-26 20:30:00 【涛涛英语学不进去】
541. 反转字符串II
给定一个字符串 s 和一个整数 k,你需要对从字符串开头算起的每隔 2k 个字符的前 k 个字符进行反转。
如果剩余字符少于 k 个,则将剩余字符全部反转。
如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。
示例:
输入: s = “abcdefg”, k = 2
输出: “bacdfeg”
就是每2*k个为一组(这点很重要!),完整的组里,前k个反转,后k个不变。剩余部分不足一半,就是k,则全部反转加进去,如果比一半多,则那k个反转即可。
有两个特殊情况,就是一共不到一组。单独拎出来处理了。
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. 反转字符串 II **/
public class ReverseStr {
public static void main(String[] args) {
System.out.println(reverseStr("abcdefg", 3));
}
public static String reverseStr(String s, int k) {
//说白了就是每 [0,k-1] 反转 [k,2k-1]不变
// 轮转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++;
}
// 多于的部分
//如果剩余字符少于 k 个,则将剩余字符全部反转。
System.out.println(result);
if (s.length() - time * 2 * k < k) {
result += reverseString(s.substring(time * 2 * k).toCharArray());
} else {
//如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。
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) {
//双指针法
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);
}
}

边栏推荐
猜你喜欢

云计算技术的发展与芯片处理器的关系

Comment installer la base de données MySQL 8.0 sous Windows? (tutoriel graphique)
![[Bayesian classification 3] semi naive Bayesian classifier](/img/9c/070638c1a613be648466e4f2bc341e.png)
[Bayesian classification 3] semi naive Bayesian classifier

Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port

uni-app使用canvas绘制二维码

【贝叶斯分类4】贝叶斯网

Tiktok practice ~ sharing module ~ generate short video QR code

【推荐收藏】这8个常用缺失值填充技巧一定要掌握

Good thing recommendation: mobile terminal development security tool

Three basic backup methods of mongodb
随机推荐
Tiktok practice ~ sharing module ~ generate short video QR code
JS mobile terminal touch screen event
Good thing recommendation: mobile terminal development security tool
[most detailed] latest and complete redis interview (42 tracks)
与 MySQL 建立连接
Keep alive cache component in Vue
MySQL recharge
When are global variables initialized before entering the main function?
【山东大学】考研初试复试资料分享
郭明錤:苹果 AR / MR 头显是其有史以来设计最复杂的产品,将于 2023 年 1 月发布
Muke 8. Service fault tolerance Sentinel
Distributed ID generation system
慕课11、微服务的用户认证与授权
MySQL - subquery usage
Super VRT
Is there any risk in opening a mobile stock registration account? Is it safe?
MySQL - table creation and management
Is it safe to open an online account in case of five-year exemption?
How to install mysql8.0 database under Windows system? (Graphic tutorial)
浏览器事件循环