Java算法之字符串反轉分析



本文來自http://blog.csdn.net/liuxian13183/ ,引用必須註明出處!java


在基本的工做內容開發中,算法不會顯得那麼重要,而在百萬級別的時候,差距很是大,今天帶你們研究下常見的字符串反轉算法。算法

public class StringReverse {
	public static String reverse1(String orig) {
		char[] s = orig.toCharArray();
		int n = s.length - 1;
		int halfLength = n / 2;
		for (int i = 0; i <= halfLength; i++) {// 2分法+替換算法
			char temp = s[i];
			s[i] = s[n - i];
			s[n - i] = temp;
		}
		return new String(s);
	}

	public static String reverse2(String s) {// 2分法+異或算法 
		char[] str = s.toCharArray();
		int begin = 0;
		int end = s.length() - 1;
		while (begin < end) {
			str[begin] = (char) (str[begin] ^ str[end]);
			str[end] = (char) (str[begin] ^ str[end]);
			str[begin] = (char) (str[end] ^ str[begin]);
			begin++;
			end--;
		}
		return new String(str);
	}

	public static String reverse3(String s) {// jdk提供
		return new StringBuffer(s).reverse().toString();
	}

	public static String reverse4(String s) {// 2分遞歸算法
		int length = s.length();
		if (length <= 1)
			return s;
		String left = s.substring(0, length / 2);
		String right = s.substring(length / 2, length);
		return reverse1(right) + reverse1(left);
	}

	public static String reverse5(String s) {// 傳統從後往前加
		int length = s.length();
		String reverse = "";
		for (int i = 0; i < length; i++)
			reverse = s.charAt(i) + reverse;
		return reverse;
	}

	public static String reverse6(String s) {//傳統與5雷同
		char[] array = s.toCharArray();
		String reverse = "";
		for (int i = array.length - 1; i >= 0; i--)
			reverse += array[i];
		return reverse;
	}

	public static String reverse7(String s) {// 利用棧先進後出的特性
		char[] str = s.toCharArray();
		Stack<Character> stack = new Stack<Character>();
		for (int i = 0; i < str.length; i++)
			stack.push(str[i]);
		String reversed = "";
		for (int i = 0; i < str.length; i++)
			reversed += stack.pop();
		return reversed;
	}

	public static void main(String[] args) {
		String str = "hello world !";
		int length = 10000000;
		long curTime = System.currentTimeMillis();
		for (int i = 0; i < length; i++) {
			reverse1(str);
		}
		System.out.println("reverse1:" + (System.currentTimeMillis() - curTime));
		curTime = System.currentTimeMillis();
		for (int i = 0; i < length; i++) {
			reverse2(str);
		}
		System.out.println("reverse2:" + (System.currentTimeMillis() - curTime));
		curTime = System.currentTimeMillis();
		for (int i = 0; i < length; i++) {
			reverse3(str);
		}
		System.out.println("reverse3:" + (System.currentTimeMillis() - curTime));
		curTime = System.currentTimeMillis();
		for (int i = 0; i < length; i++) {
			reverse4(str);
		}
		System.out.println("reverse4:" + (System.currentTimeMillis() - curTime));
		curTime = System.currentTimeMillis();
		for (int i = 0; i < length; i++) {
			reverse5(str);
		}
		System.out.println("reverse5:" + (System.currentTimeMillis() - curTime));
		curTime = System.currentTimeMillis();
		for (int i = 0; i < length; i++) {
			reverse6(str);
		}
		System.out.println("reverse6:" + (System.currentTimeMillis() - curTime));
		curTime = System.currentTimeMillis();
		for (int i = 0; i < length; i++) {
			reverse7(str);
		}
		System.out.println("reverse7:" + (System.currentTimeMillis() - curTime));
	}
}

相關文章
相關標籤/搜索