/** * */ package com.wsheng.aggregator.algorithm.string; import java.util.Stack; /** * 8 种字符串反转的方法, 其实可以是9种方法,第9种是使用StringBuffer和StringBuilder中实现的方法 * @author Josh Wang(Sheng) * * @email [email protected] * */ public class StringReverse { /** * 二分递归地将后面的字符和前面的字符连接起来。 * * @param s * @return */ public static String reverse1(String s) { 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); } /** * 取得当前字符并和之前的字符append起来 * @param s * @return */ public static String reverse2(String s) { int length = s.length(); String reverse = ""; for (int i=0; i<length; i++) reverse = s.charAt(i) + reverse; return reverse; } /** * 将字符从后往前的append起来 * @param s * @return */ public static String reverse3(String s) { char[] array = s.toCharArray(); String reverse = ""; for (int i = array.length - 1; i >= 0; i--) { reverse += array[i]; } return reverse; } /** * 和StringBuffer()一样,都用了Java自实现的方法,使用位移来实现 * @param s * @return */ public static String reverse4(String s) { return new StringBuilder(s).reverse().toString(); } /** * 和StringBuilder()一样,都用了Java自实现的方法,使用位移来实现 * @param s * @return */ public static String reverse5(String s) { return new StringBuffer(s).reverse().toString(); } /** * 二分交换,将后面的字符和前面对应的那个字符交换 * @param s * @return */ public static String reverse6(String s) { char[] array = s.toCharArray(); int end = s.length() - 1; int halfLength = end / 2; for (int i = 0; i <= halfLength; i++) { char temp = array[i]; array[i] = array[end-i]; array[end-i] = temp; } return new String(array); } /** * 原理是使用异或交换字符串 * a=a^b; * b=b^a; * a=b^a; * * @param s * @return */ public static String reverse7(String s) { char[] array = s.toCharArray(); int begin = 0; int end = s.length() - 1; while (begin < end) { array[begin] = (char) (array[begin] ^ array[end]); array[end] = (char) (array[end] ^ array[begin]); array[begin] = (char) (array[end] ^ array[begin]); begin++; end--; } return new String(array); } /** * 基于栈先进后出的原理 * * @param s * @return */ public static String reverse8(String s) { char[] array = s.toCharArray(); Stack<Character> stack = new Stack<Character>(); for (int i = 0; i < array.length; i++) stack.push(array[i]); String reverse = ""; for (int i = 0; i < array.length; i++) reverse += stack.pop(); return reverse; } public static void main(String[] args) { System.out.println(reverse1("Wang Sheng")); System.out.println(reverse2("Wang Sheng")); System.out.println(reverse3("Wang Sheng")); System.out.println(reverse4("Wang Sheng")); System.out.println(reverse5("Wang Sheng")); System.out.println(reverse6("Wang Sheng")); System.out.println(reverse7("Wang Sheng")); System.out.println(reverse8("Wang Sheng")); } }
来自:http://josh-persistence.iteye.com/blog/2205772
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/10287.html