5.5 左旋转字符串(串)——【LeetCode】

5.5 左旋转字符串(串)——【LeetCode】_第1张图片

public String reverseLeftWords(String s, int n) {
		int len = s.length();
		StringBuilder sb = new StringBuilder(s);
		reverseString(sb, 0, n-1);
		reverseString(sb, n, len-1);
		
		return sb.reverse().toString();
	}

	private void reverseString(StringBuilder sb, int start, int end) {
		while(start < end) {
			char temp = sb.charAt(start);
			sb.setCharAt(start, sb.charAt(end));
			sb.setCharAt(end, temp);
			start++;
			end--;
		}
		
	}

你可能感兴趣的:(leetcode,leetcode,算法,职场和发展)