当前位置:网站首页>Leetcode- reverse string ii- simple
Leetcode- reverse string ii- simple
2022-06-13 05:49:00 【AnWenRen】
title :541 Reverse string - Simple
subject
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 .
Example 1
Input :s = "abcdefg", k = 2
Output :"bacdfeg"
Example 2
Input :s = "abcd", k = 2
Output :"bacd"
Tips
1 <= s.length <= 104
s
It consists of lowercase English only1 <= k <= 104
Code Java
public String reverseStr(String s, int k) {
StringBuilder sb = new StringBuilder();
int count = 0; // Record the start position of the reversal
for (int i = 0, j = 1, g = 1; i < s.length(); i++) {
if (i < k * j) {
sb.insert(count, s.charAt(i));
} else if (i < k * g * 2) {
sb.append(s.charAt(i));
} else {
j += 2;
g ++;
count = i;
i--;
}
}
return sb.toString();
}
边栏推荐
- Zero copy technology
- NVIDIA Jetson nano/xavier NX capacity expansion tutorial
- Leetcode- longest palindrome string - simple
- Concurrent programming -- source code analysis of thread pool
- Randomly fetch data from the list
- 11 signalthrowingevent and signalboundaryevent of flowable signal event
- MongoDB 多字段聚合Group by
- Timeout thread log for tongweb
- Browser screenshot method (long screenshot, node screenshot, designated area screenshot)
- August 15, 2021 another week
猜你喜欢
软件测试——接口常见问题汇总
Mongodb Multi - field Aggregation group by
Use the browser to cut the entire page (take chrome as an example)
890. Find and Replace Pattern
Sentinel series integrates Nacos and realizes dynamic flow control
About Evaluation Metrics
About Evaluation Metrics
2021.9.30学习日志-postman
What happens when the MySQL union index ABC encounters a "comparison operator"?
2021.9.30 learning log -postman
随机推荐
MySQL fuzzy query and sorting by matching degree
Leetcode- first unique character in string - simple
Fast power code
Power simple of leetcode-3
Interrupt processing
Integer tips
Leetcode- third largest number - simple
Top slide immersive dialog
Conf/tongweb Functions of properties
Zero copy technology
Django uses redis to store sessions starting from 0
2021.9.30学习日志-postman
ArrayList loop removes the pit encountered
Leetcode guessing numbers game - simple
A simple recursion problem of linked list
使用cmake交叉編譯helloworld
No assessment summary
2021.9.30 learning log -postman
Ffmpeg download suffix is Video files for m3u8
Validation set: ‘flowable-executable-process‘ | Problem: ‘flowable-servicetask-missing-implementatio