当前位置:网站首页>力扣541-反转字符串2——双指针法
力扣541-反转字符串2——双指针法
2022-07-30 05:09:00 【张怼怼√】
题目描述
给定一个字符串 s 和一个整数 k,从字符串开头算起,每计数至 2k 个字符,就反转这 2k 字符中的前 k 个字符。
如果剩余字符少于 k 个,则将剩余字符全部反转。
如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。
解题思路
这个题目和上一篇反转字符串是相同解法的
力扣344-反转字符串——双指针法力扣344-反转字符串——双指针法https://blog.csdn.net/weixin_44564247/article/details/126037243?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22126037243%22%2C%22source%22%3A%22weixin_44564247%22%7D&ctrtid=dkBHs在本题中只是将字符串划分为2*k段,并对每段中的k段进行反转。
这里遍历字符串的时候,为了方便,可以将字符串转换为字符数组,遍历的时候 i 的取值每次不是i++,而且 i += 2*k ,然后在每个子区间内定义两个指针指向子区间的首尾,利用双指针法对其进行交换。
输入输出示例

代码
class Solution {
public String reverseStr(String s, int k) {
char[] charr = s.toCharArray();
int len = s.length();
char tem;
for(int i = 0; i < len; i +=2*k){
int start = i;
int stop = Math.min(start+k-1,len-1);
while(start <= stop){
tem = charr[start];
charr[start] = charr[stop];
charr[stop] = tem;
start++;
stop--;
}
}
return new String(charr);
}
}边栏推荐
- ThinkPHP high imitation blue play cloud network disk system source code / docking easy payment system program
- SVN View Username and Password
- Detailed explanation of REUSE_ALV_GRID_DISPLAY
- Web page element parsing a tag
- [3D Detection Series-PointRCNN] Reproduces the PointRCNN code, and realizes the visualization of PointRCNN3D target detection, including the download link of pre-training weights (starting from 0 and
- 小程序使用npm包定制全局样式
- L2-020 功夫传人
- C language implements highly secure game archives and reads files
- Hexagon_V65_Programmers_Reference_Manual (14)
- [Vitis] Code implementation of ZCU102 development board PS-side control PL-side reset
猜你喜欢

“幻核”跑路,数字藏品路在何方?

Seven, custom configuration

WPF introduces ttf icon file usage record

Protobuf compound data types, speaking, reading and writing

3. Dependency configuration management
![[High Performance Computing] openMP](/img/a5/2cfd760a26edb379d337eb3d1605d5.jpg)
[High Performance Computing] openMP

QT(39)-vs development qt program prompts that the source file cannot be opened

1315_Use the LOOPBACK simulation mode to test whether the pyserial installation is successful

Whole process scheduling - Azkaban entry and advanced

Kyligence 亮相第五届南方信息大会并获评“CIO 优选数字化服务商”
随机推荐
C语言实现安全性极高的游戏存档并读档
String Problem (Part 1)
Acwing完全数
How can I make (a == 1 && a == 2 && a == 3) to be true?
三、依赖配置管理
(Hexagon_V65_Programmers_Reference_Manual(13)
The Double Pointer Problem (Part 2)
Recursive Optimization of Fibonacci Sequences "Memo Recursion"
pytorch官网中如何选择以及后面的安装和pycharm测试步骤
C语言中的基本库函数(qsort)
Catch That Cow(详解)
Double pointer problem (middle)
[Android development] Splash interface / user agreement and privacy policy pop-up window / interface development
[High Performance Computing] openMP
Summary of skills in using ms project2010 project management software
Kyligence 亮相第五届南方信息大会并获评“CIO 优选数字化服务商”
WPF study notes "WPF Layout Basics"
22. 为什么需要消息队列?使用消息队列有什么好处?
给小白的 PG 容器化部署教程(下)
uni-app realizes cross-end development of mobile phone Bluetooth to receive and send data