当前位置:网站首页>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();
}
边栏推荐
- 2 first experience of drools
- 计算两个时间相差的天数(支持跨月、跨年)
- C calls the API and parses the returned JSON string
- Concurrent programming -- countdownlatch combined with thread pool
- 2021.9.29学习日志-Restful架构
- Django uses redis to store sessions starting from 0
- Config server configuration center of Nacos series
- Function and application scenario of field setaccessible() method
- Mongodb Multi - field Aggregation group by
- Unity游戏优化(第2版)学习记录7
猜你喜欢
2 first experience of drools
Misunderstanding of tongweb due to ease of use
The reason why the process cannot be shut down after a spark job is executed and the solution
13 cancelendevent of a flowable end event and compensationthrowing of a compensation event
Comment procéder à l'évaluation des algorithmes
Function and application scenario of field setaccessible() method
Interrupt processing
2021.9.30学习日志-postman
Information collection for network security (2)
Django uploads local binaries to the database filefield field
随机推荐
15 inclusivegateway and eventgateway of flowable gateway
Validation set: ‘flowable-executable-process‘ | Problem: ‘flowable-servicetask-missing-implementatio
Pychart encountered time zone problem when connecting to MySQL timezone
[China & some provinces and cities] JSON file for offline map visualization
About Evaluation Metrics
A fast week
MongoDB 多字段聚合Group by
Implementation of concurrent programming locking
使用cmake交叉编译helloworld
AUTOSAR实战教程pdf版
2021.9.29 learning log restful architecture
Leetcode- reverse vowels in string - simple
Leetcode- string addition - simple
About the solution of pychart that cannot be opened by double clicking
Mobile end adaptation scheme
Leetcode- intersection of two arrays - simple
Getclassloader() returns null, getclassloader() gets null
Concurrent programming -- countdownlatch combined with thread pool
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
Leetcode guessing numbers game - simple