当前位置:网站首页>String - 541. Reverse string II
String - 541. Reverse string II
2022-07-24 11:29:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
- Reverse string II
Given a string s And an integer k, From the beginning of the string , Each count to 2k Characters , Just reverse this 2k The first... In the character k Characters .
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 .
2 Title Example
Example 1:
Input :s = “abcdefg”, k = 2
Output :“bacdfeg”
Example 2:
Input :s = “abcd”, k = 2
Output :“bacd”
3 Topic tips
1 <= s.length <= 104
s It consists of lowercase English only
1 <= k <= 104
4 Ideas
Method 1 : simulation
Let's simulate directly according to the meaning of the question : Invert each subscript from 2k2k Starting with a multiple of , The length is kk The string of . If the length of the substring is insufficient kk, Then invert the whole substring .
Some students may be dealing with logic : every other 2k Before characters k The characters of , Write a bunch of logic code or make a counter , To statistics 2k, Before statistics k Characters .
In fact, in the process of traversing the string , As long as you make i += (2 * k),i Each move 2 * k That's all right. , And then determine whether there is a reverse interval .
Because what we are looking for is every 2 * k Starting point of interval , Write it like this , The program will be much more efficient .
So when it's time to deal with strings in a regular way , Think about it in for Do something on the expression of the loop .
Complexity analysis
Time complexity :O(n), among n Is string s The length of .
Spatial complexity :O(1) or O(n), Depending on the nature of the string type in the language used . If the string is modifiable , Then we can directly modify the original string , The space complexity is O(1), Otherwise you need to use O(n) Space to temporarily convert the string into a data structure that can be modified ( For example, an array of ), The space complexity is O(n).
5 My answer
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--;
}
}
}
// Solution 2 ( It seems easier to understand )
// In fact, the meaning of the title is every other 2k Before the first reversal k individual , Not enough mantissa k All reverse at one time
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;
// Here is to judge whether the mantissa is enough k It depends end The position of the pointer
int end = Math.min(ch.length - 1, start + k - 1);
// Invert with XOR
while(start < end){
ch[start] ^= ch[end];
ch[end] ^= ch[start];
ch[start] ^= ch[end];
start++;
end--;
}
}
return new String(ch);
}
}
边栏推荐
- Leetcode 112. 路径总和
- MicroBlaze adds a custom IP core and attaches the Axi bus to realize ssd1306 OELD drive
- Robot Framework官方教程(一)入门
- Easy to understand ES6 (IV): template string
- [deserialization vulnerability-01] Introduction to serialization and deserialization
- What is the charm of CSDN members? What's the use of him?
- [golang] before method of time type in golang
- 系统管理员需知的 16 个 iptables 使用技巧
- Use of getchar
- Jmeter-Runtime控制器
猜你喜欢
](/img/1f/37c5548ce84b6a217b4742431f1cc4.png)
运算放大器 —— 快速复苏笔记[壹](参数篇)
](/img/fd/e12f43e23e6ec76c2b44ce7813e204.png)
运算放大器 —— 快速复苏笔记[贰](应用篇)
![[deserialization vulnerability-01] Introduction to serialization and deserialization](/img/e4/6b9ee6ee74f3cdc3c886ed3af9ef73.png)
[deserialization vulnerability-01] Introduction to serialization and deserialization

JMeter runtime controller

Reprint of illustrations in nature, issue 3 - area map (part2-100)

CSDN会员的魅力何在?我要他有什么用?

Sorting out the ideas of data processing received by TCP server, and the note of select: invalid argument error

DevOps及DevOps常用的工具介绍

HCIP MGRE实验 第三天
![About [software testing - interview skills and precautions for automated testing] - talk freely](/img/c2/bd1a52bdd7ab07878348b6216012f0.png)
About [software testing - interview skills and precautions for automated testing] - talk freely
随机推荐
Robot framework official tutorial (I) getting started
In BS4.String and Difference between text
[golang] golang implements simple Memcache
Lanqiao cup provincial training camp - commonly used STL
【Golang】golang中time类型的before方法
Win10 icon turns white, recovery method
Leetcode 112. path sum
Fiddler抓包工具总结
How to use SSH and SFTP protocols at home
Depth first search and breadth first search of Graphs
[golang] before method of time type in golang
PDF处理还收费?不可能!
哈希——15. 三数之和
JMeter interface test steps - Installation Tutorial - script recording - concurrent test
Sentinel vs Hystrix 限流对比,到底怎么选?
08 [AIO programming]
Stack Title: basic calculator II
链表——142. 环形链表 II
【Golang】golang实现urlencode urldecode函数
What is the difference between low code and no code?