Created
November 26, 2014 13:29
-
-
Save huanggm/332e507966bff8efa88b to your computer and use it in GitHub Desktop.
给定一个字符串,要求把字符串前面的若干个字符移动到字符串的尾部,如把字符串“abcdef”前面的2个字符'a'和'b'移动到字符串的尾部,使得原字符串变成字符串“cdefab”。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverse(s, k) { | |
var arr = s.split(""), l = s.length; k = k % l; | |
function r(m, n) { | |
while(m < n) { | |
var tmp = arr[m]; | |
arr[m++] = arr[n]; | |
arr[n--] = tmp; | |
} | |
} | |
r(0, k - 1); | |
r(k, l - 1); | |
r(0, l - 1); | |
return arr.join(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment