Skip to content

Commit

Permalink
update 0541.反转字符串II:补充注释
Browse files Browse the repository at this point in the history
  • Loading branch information
juguagua authored Nov 25, 2022
1 parent 45dad67 commit 52b0757
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion problems/0541.反转字符串II.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ func reverseStr(s string, k int) string {
ss := []byte(s)
length := len(s)
for i := 0; i < length; i += 2 * k {
// 1. 每隔 2k 个字符的前 k 个字符进行反转
// 2. 剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符
if i + k <= length {
reverse(ss[i:i+k])
} else {
Expand Down Expand Up @@ -326,7 +328,7 @@ javaScript:
var reverseStr = function(s, k) {
const len = s.length;
let resArr = s.split("");
for(let i = 0; i < len; i += 2 * k) {
for(let i = 0; i < len; i += 2 * k) { // 每隔 2k 个字符的前 k 个字符进行反转
let l = i - 1, r = i + k > len ? len : i + k;
while(++l < --r) [resArr[l], resArr[r]] = [resArr[r], resArr[l]];
}
Expand Down

0 comments on commit 52b0757

Please sign in to comment.