avatar

superorange0707

  • Homepage
  • Tag
  • Category
Home Leetcode-541-Reverse String 2
文章

Leetcode-541-Reverse String 2

Posted 2022-07-10
5~6 min read

**leetcode:**https://leetcode.com/problems/reverse-string-ii

Description:

Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.

If there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.

Idea:

Step1: because the problem need to change the range between k character and 2k characters, we traverse the string and increase the index by 2k

Step2: if left characters less than k characters, it means the current index + k will larger than the length of string, so reverse the whole left string

Step3: if there are less than 2k but greater than or equal to k characters, then reverse the first k characters. it menas if the current index plus k won’t exceed the length of string, just reverse the first k character. the range of the reverse is(i, i+k-1)

Code:

class Solution {
    public String reverseStr(String s, int k) {
        int length = s.length()-1;
        char[] result = s.toCharArray();
//         traverse the string and set the index increased by 2k each time
        for(int i=0; i<=length; i = i+2*k){
//         if the left characters more than k characters(so the current index + k, it will less or equal than length of string), reverse first k
            if (i+k<=length){
                reverse(result, i, i+k-1);
                continue;
            }
//          if left characters less than k characters, reverse all
            if(i+k > length){
                reverse(result, i, length);
                continue;
            }
        }
        return new String(result);
    }
    public void reverse(char[]s, int i, int j){
        while(i<j){
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            i++;
            j--;
        }
    }
}

return the character array to string

new String(character);
Leetcode
Leetcode String
Share

Further Reading

Apr 23, 2025

283 - Move Zero

[283 - Move Zero] 🔗 LeetCode Link Problem Description Given an integer array nums, move all 0's to the end of it while maintaining the relative order

Apr 23, 2025

27 - Remove Element

[27 - Remove Element] 🔗 LeetCode Link Problem Description Given an integer array nums and an integer val, remove all occurrences of val in nums in-pl

Apr 23, 2025

26 - Remove Duplicates from Sorted Array

[26 - Remove Duplicates from Sorted Array] 🔗 LeetCode Link Problem Description Given an integer array nums sorted in non-decreasing order, remove the

OLDER

Leetcode-344-Reverse String

NEWER

Leetcode-Replace Space in the String

Recently Updated

  • Migrating Jenkins SCM Using GitLab from Bitbucket: SCM URL Bulk Replacement
  • 283 - Move Zero
  • 27 - Remove Element
  • 26 - Remove Duplicates from Sorted Array
  • Migrating from Bitbucket to GitLab? Here’s how to keep your teams moving without missing a beat!

Trending Tags

Course two pointer Binary Tree Hash SQL Leetcode Error Recording Gitlab Bitbucket Devops

Contents