现在的位置: 首页 > 综合 > 正文

字符串左旋

2017年12月04日 ⁄ 综合 ⁄ 共 481字 ⁄ 字号 评论关闭
/**
	 * @author PLA 字符串左旋
	 */

	public static void main(String[] args) {
		String s = "abcdefghi";
		char[] ch = s.toCharArray();
		int m = 3;
		System.out.println("原字符串:" + s);
		swap(ch, 0, m);
		swap(ch, m, s.length());
		swap(ch, 0, s.length());
		System.out.println("左旋"+m+"位:");
		System.out.println(ch);
	}

	public static char[] swap(char[] ch, int m1, int m2) {
		if (m1 > ch.length) {
			System.out.println("Error!");
		}
		int begin, end;
		char temp;
		for (begin = m1, end = m2 - 1; begin < (begin + end + 1) / 2; begin++, end--) {
			temp = ch[begin];
			ch[begin] = ch[end];
			ch[end] = temp;
		}
		return ch;
	}

抱歉!评论已关闭.