8.Rotate String

public class Solution {
/**
 * @param str: an array of char
 * @param offset: an integer
 * @return: nothing
 */
private void swap(char[] str, int start, int end) {
    char a = str[start];
    str[start] = str[end];
    str[end] = a;
}

private void reverse(char[] str, int start, int end) {
    while (start < end) {
        swap(str, start, end);
        start++;
        end--;
    }
}
public void rotateString(char[] str, int offset) {
    // write your code here
    if (str == null) return;
    int length = str.length;
    if (length == 0) return;
    int position = length - offset % length - 1;
    reverse(str, 0, position);
    reverse(str, position + 1, length - 1);
    reverse(str, 0, length - 1);
}
}

좋은 웹페이지 즐겨찾기