Java逆转字符串

用递归的方法逆转字符串,网上的方法很多,我都试了一下,代码是用Java写的。

import java.util.Scanner;
import java.util.Stack;

/*
 * 考的问题是 3 个算法题,都是在一个源文件里,12 个用例,开始就送 5 个用例,
 * 以最后一次提交的得分为准。上机题目不是很难,一个是用递归的方法实现字符串逆置,
 * 一个是返回字符串中空格的数量,一个是罗马数字和阿拉伯数字转换的题,
 * 这题以前考过,有人很快就写好了,当然也有人上机确实不好,这 50 分一定要好好争取。
 * 引用自王道论坛的回忆http://cskaoyan.com/thread-649681-1-2.html
 */

public class K18_1 {
	// 用递归的方法实现字符串的逆转
	public static String reverse0_0(String str) {
		if (str.length() == 0 || str.length() == 1 || str == null)
			return str;
		else
			return reverse0_0(str.substring(1)) + str.charAt(0);
	}

	// 二分递归
	public static String reverse0_1(String str) {
		int length = str.length();
		if (length <= 1)
			return str;
		String left = str.substring(0, length / 2);
		String right = str.substring(length / 2, length);
		return reverse0_1(right) + reverse0_1(left);
	}

	// ---其他方法---
	// 使用StringBuilder 或者StringBuffer自带的方法实现
	public static String reverse1(String str) {
		return new StringBuilder(str).reverse().toString();
		// return new StringBuffer(str).reverse().toString();
	}

	// 用栈这个数据结构实现逆转
	public static String reverse2(String str) {
		char[] str_array = str.toCharArray();
		Stack<Character> stack = new Stack<Character>();
		for (int i = 0; i < str_array.length; i++) {
			stack.push(str_array[i]);
		}

		StringBuilder str_new = new StringBuilder();
		for (int i = 0; i < str_array.length; i++) {
//			System.out.println(i + ":" + stack.pop());
			str_new.append(stack.pop());

		}
		return str_new.toString();
	}

	// 把字符串每个字符顺序取出来,放到最前
	public static String reverse3(String str) {
		StringBuilder str_new = new StringBuilder();
		for (int i = 0; i < str.length(); i++) {
			str_new.insert(0, str.charAt(i));
		}
		return str_new.toString();
	}

	// 把字符串每个字符逆序取出来
	public static String reverse4(String str) {
		StringBuilder str_new = new StringBuilder();
		for (int i = str.length() - 1; i > -1; i--) {
			str_new.append(str.charAt(i));
		}
		return str_new.toString();
	}

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			String string = (String) cin.next();
			System.out.println("the result of function0_0 = " + reverse0_0(string));
			System.out.println("the result of function0_1 = " + reverse0_1(string));
			System.out.println("the result of function1 = " + reverse1(string));
			System.out.println("the result of function2 = " + reverse2(string));
			System.out.println("the result of function3 = " + reverse3(string));
			System.out.println("the result of function4 = " + reverse4(string));
		}
	}
}

run一下:

abcd
the result of function0_0 = dcba
the result of function0_1 = dcba
the result of function1 = dcba
the result of function2 = dcba
the result of function3 = dcba
the result of function4 = dcba
12345
the result of function0_0 = 54321
the result of function0_1 = 54321
the result of function1 = 54321
the result of function2 = 54321
the result of function3 = 54321
the result of function4 = 54321
54321
the result of function0_0 = 12345
the result of function0_1 = 12345
the result of function1 = 12345
the result of function2 = 12345
the result of function3 = 12345
the result of function4 = 12345
1234
the result of function0_0 = 4321
the result of function0_1 = 4321
the result of function1 = 4321
the result of function2 = 4321
the result of function3 = 4321
the result of function4 = 4321

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值