用递归的方法逆转字符串,网上的方法很多,我都试了一下,代码是用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