当前位置:网站首页>字符串——541. 反转字符串 II
字符串——541. 反转字符串 II
2022-07-24 11:23:00 【向着百万年薪努力的小赵】
1 题目描述
- 反转字符串 II
给定一个字符串 s 和一个整数 k,从字符串开头算起,每计数至 2k 个字符,就反转这 2k 字符中的前 k 个字符。
如果剩余字符少于 k 个,则将剩余字符全部反转。
如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。
2 题目示例
示例 1:
输入:s = “abcdefg”, k = 2
输出:“bacdfeg”
示例 2:
输入:s = “abcd”, k = 2
输出:“bacd”
3 题目提示
1 <= s.length <= 104
s 仅由小写英文组成
1 <= k <= 104
4 思路
方法一:模拟
我们直接按题意进行模拟:反转每个下标从 2k2k 的倍数开始的,长度为 kk 的子串。若该子串长度不足 kk,则反转整个子串。
一些同学可能为了处理逻辑:每隔2k个字符的前k的字符,写了一堆逻辑代码或者再搞一个计数器,来统计2k,再统计前k个字符。
其实在遍历字符串的过程中,只要让 i += (2 * k),i 每次移动 2 * k 就可以了,然后判断是否需要有反转的区间。
因为要找的也就是每2 * k 区间的起点,这样写,程序会高效很多。
所以当需要固定规律一段一段去处理字符串的时候,要想想在在for循环的表达式上做做文章。
复杂度分析
时间复杂度:O(n),其中 n 是字符串 s 的长度。
空间复杂度:O(1) 或 O(n),取决于使用的语言中字符串类型的性质。如果字符串是可修改的,那么我们可以直接在原字符串上修改,空间复杂度为 O(1),否则需要使用 O(n) 的空间将字符串临时转换为可以修改的数据结构(例如数组),空间复杂度为 O(n)。
5 我的答案
class Solution {
public String reverseStr(String s, int k) {
int n = s.length();
char[] arr = s.toCharArray();
for (int i = 0; i < n; i += 2 * k) {
reverse(arr, i, Math.min(i + k, n) - 1);
}
return new String(arr);
}
public void reverse(char[] arr, int left, int right) {
while (left < right) {
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
}
//解法二(似乎更容易理解点)
//题目的意思其实概括为 每隔2k个反转前k个,尾数不够k个时候全部反转
class Solution {
public String reverseStr(String s, int k) {
char[] ch = s.toCharArray();
for(int i = 0; i < ch.length; i += 2 * k){
int start = i;
//这里是判断尾数够不够k个来取决end指针的位置
int end = Math.min(ch.length - 1, start + k - 1);
//用异或运算反转
while(start < end){
ch[start] ^= ch[end];
ch[end] ^= ch[start];
ch[start] ^= ch[end];
start++;
end--;
}
}
return new String(ch);
}
}
边栏推荐
- [golang] golang implements sha256 encryption function
- [golang] golang implements simple Memcache
- How to choose sentinel vs. hystrix current limiting?
- Blue Bridge Cup - binary conversion exercise
- Decomposition of kubernets principle
- Code of login page
- Ldr6028 charging OTG live line live sound card audio adapter is the most cost-effective solution
- Simply understand MODBUS function code and partition
- [golang] golang realizes sending wechat service number template messages
- How to use SSH and SFTP protocols at home
猜你喜欢
![08 [AIO programming]](/img/a6/156cb97e653190c76f22c88b758fef.png)
08 [AIO programming]

如何从功能测试到自动化测试?

Jmeter-Runtime控制器

Directional crawling Taobao product name and price (teacher Songtian)

视频回放 | 如何成为一名优秀的地学和生态学领域的国际期刊审稿人?

HCIP MGRE实验 第三天

关于【软件测试-自动化测试之面试技巧和注意事项】——侃侃而谈

自学软件测试天赋异禀——不是盖的

Over the weekend, I had a dinner with the technology gurus and talked about the "golden nine and silver ten" peak of the software testing industry [the trend of involution has been formed]

How to convert word to markdown text
随机推荐
【Golang】golang实现简单memcache
[golang] golang实现截取字符串函数SubStr
《Nature》论文插图复刻第3期—面积图(Part2-100)
Fiddler packet capture tool summary
[golang] golang implements the string interception function substr
[deserialization vulnerability-02] principle test and magic method summary of PHP deserialization vulnerability
Why can't memset initialize array elements to 1?
Leetcode 112. 路径总和
Depth first search and breadth first search of Graphs
The number of digits of power and factorial
ctfshow ThinkPHP专题 1
Docker builds MySQL master-slave replication
【反序列化漏洞-01】序列化与反序列化简介
自学软件测试天赋异禀——不是盖的
MicroBlaze adds a custom IP core and attaches the Axi bus to realize ssd1306 OELD drive
RRPN:Arbitrary-Oriented Scene Text Detection via Rotation Proposals
【反序列化漏洞-02】PHP反序列化漏洞原理测试及魔术方法总结
Robot framework official tutorial (I) getting started
[golang] deletion and emptying of map elements in golang
性能测试总结(一)---基础理论篇