当前位置:网站首页>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);
}
}

边栏推荐
- The goal you specified requires a project to execute but there is no POM
- c语言99乘法表
- SentinelResource注解詳解
- 0基础c语言(0)
- Daily basic use of alicloud personal image warehouse
- Détails de l'annotation des ressources sentinelles
- The two files are merged into a third file.
- C language file cursor fseek
- 回溯思路详解
- C primer plus learning notes - 3. Character IO (input / output)
猜你喜欢
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data

Database SQL statement writing

Tiktok practice ~ sharing module ~ copy short video link

MySQL - database creation and management

慕课8、服务容错-Sentinel

Uni app uses canvas to draw QR code

清华大学就光刻机发声,ASML立马加紧向中国出口光刻机

威胁猎人必备的六个威胁追踪工具

C language 99 multiplication table

Detailed explanation of shutter textfield
随机推荐
BOM and DOM operations
Feitian +cipu body brings more imagination to the metauniverse
Uni app uses canvas to draw QR code
开发者调查:Rust/PostgreSQL 最受喜爱,PHP 薪水偏低
Selection of database paradigm and main code
Is there any risk in opening a mobile stock registration account? Is it safe?
与 MySQL 建立连接
慕课8、服务容错-Sentinel
定长内存池
Unity - URP get camera stack
Disruptor本地线程队列_使用transProcessor处理器和WorkPool两种方式进行消费对比---线程间通信工作笔记005
【贝叶斯分类3】半朴素贝叶斯分类器
vue中缓存组件keep-alive
浏览器的垃圾回收机制
股票开户的具体步骤是什么?网上开户安全吗?
Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
2022/02/14 line generation
超分之VRT
Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data
MySQL中存储过程的详细详解