1题目:给一个字符串,反向输出:
例如:“hello”,输出“olleh”
2代码:
package com.leetcode;
import java.util.Scanner;
import java.util.Stack;
public class C344 {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("please input String:");
String str = in.next();
System.out.println(reverse6(str));
}
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);
}
public static String reverse2(String s) {
char[] chars = new char[s.length()];
int index = 0;
for(int i = s.length() - 1;i>=0;i--) {
chars[index++] = s.charAt(i);
}
return new String(chars);
}
public static String reverse3(String s) {
String reverse = "";
for(int i = s.length() -1;i>=0;i--) {
reverse += s.charAt(i);
}
return reverse;
}
public static String reverse4(String s) {
return new StringBuffer(s).reverse().toString();
}
public static String reverse5(String s) {
char[] chars = s.toCharArray();
int n = chars.length-1;
for(int i=0;i<=n/2;i++) {
char tem = chars[i];
chars[i] = chars[n-i];
chars[n-i] = tem;
}
return new String(chars);
}
public static String reverse6(String s) {
char[] chars = s.toCharArray();
int begin = 0;
int end = chars.length - 1;
while (begin < end ) {
chars[begin] = (char) (chars[begin] ^ chars[end]);
chars[end] = (char) (chars[begin] ^ chars[end]);
chars[begin] = (char) (chars[begin] ^ chars[end]);
begin++;
end--;
}
return new String(chars);
}
public static String reverse7(String s){
char[] chars = s.toCharArray();
Stack<Character> stack = new Stack<>();
String reverse = "";
for(int i = 0;i<chars.length;i++) {
stack.push(chars[i]);
}
for(int i = 0;i<chars.length;i++) {
reverse += stack.pop();
}
return reverse;
}
}
3分析: java解决这个问题方法很容易想到多种,以上就是我列出来的7种方法。
但大体可以分为三类:利用java已有的封装好的方法、利用交换、利用堆栈。
(1)利用java已有的封装好的方法
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);
}
public static String reverse2(String s) {
char[] chars = new char[s.length()];
int index = 0;
for(int i = s.length() - 1;i>=0;i--) {
chars[index++] = s.charAt(i);
}
return new String(chars);
}
public static String reverse3(String s) {
String reverse = "";
for(int i = s.length() -1;i>=0;i--) {
reverse += s.charAt(i);
}
return reverse;
}
public static String reverse4(String s) {
return new StringBuffer(s).reverse().toString();
}
(2)利用交换
public static String reverse5(String s) {
char[] chars = s.toCharArray();
int n = chars.length-1;
for(int i=0;i<=n/2;i++) {
char tem = chars[i];
chars[i] = chars[n-i];
chars[n-i] = tem;
}
return new String(chars);
}
public static String reverse6(String s) {//效率会高一点,不费空间实现交换
char[] chars = s.toCharArray();
int begin = 0;
int end = chars.length - 1;
while (begin < end ) {
chars[begin] = (char) (chars[begin] ^ chars[end]);
chars[end] = (char) (chars[begin] ^ chars[end]);
chars[begin] = (char) (chars[begin] ^ chars[end]);
begin++;
end--;
}
return new String(chars);
}
(3)利用堆栈
public static String reverse7(String s){
char[] chars = s.toCharArray();
Stack<Character> stack = new Stack<>();
String reverse = "";
for(int i = 0;i<chars.length;i++) {
stack.push(chars[i]);
}
for(int i = 0;i<chars.length;i++) {
reverse += stack.pop();
}
return reverse;
}