Skip to content

Latest commit

 

History

History
32 lines (27 loc) · 863 Bytes

Question_541.md

File metadata and controls

32 lines (27 loc) · 863 Bytes

LeetCode Records - Question 541 Reverse String II

Attempt 1: Use a helper function to reverse the characters in the array

class Solution {
    public String reverseStr(String s, int k) {
        char[] arr = s.toCharArray();
        int twoK = 2 * k;

        int i = 0;
        for (; i + twoK <= arr.length; i += twoK) {
            reverseArr(arr, i, i + k);
        }
        reverseArr(arr, i, Math.min(arr.length, i + k));

        return new String(arr);
    }

    private void reverseArr(char[] arr, int start, int end) {
        int step = (end - start) / 2;
        for (int i = 0; i < step; i++) {
            char temp = arr[start + i];
            arr[start + i] = arr[end - i - 1];
            arr[end - i - 1] = temp;
        }
    }
}
  • Runtime: 1 ms (Beats: 83.24%)
  • Memory: 43.83 MB (Beats: 18.87%)