字符串--------循环左移

已知字符数组  abcdef

循环左移2位  输出cdefab


方法:三次反转


public class Solution {
	public static void main(String[] args) {
		char[] s=new char[]{'a','b','c','d','e','f'};
		leftReverse(s, 6, 2);
		System.out.println(new String(s));
	}
	//已字符数组起点和终点的反转 必须记住!
	public static void Reverse(char[] s,int st,int ed){
		while(st<ed)
		{
			char temp=s[st];
			s[st++]=s[ed];
			s[ed--]=temp;
		}
	} 
	//三次反转的方法
	public static void leftReverse(char[] s,int n,int m){
		Reverse(s, 0, m-1);
		Reverse(s, m, n-1);
		Reverse(s, 0, n-1);
	}
}


猜你喜欢

转载自blog.csdn.net/ustcyy91/article/details/80099159