# 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));
}
}


A1 A2 A3
B1 B2 B3
C1 C2 C3
3场第1名赛一场，得出第1名

1000*（1+1/3+1/5）-1000=1600/3吨，即533.33吨。功夫不负有心人，这就是最终答案！

|
21天前
|

GEE时序——利用sentinel-2（哨兵-2）数据进行地表物候学分析（时间序列平滑法估算和非平滑算法代码）
GEE时序——利用sentinel-2（哨兵-2）数据进行地表物候学分析（时间序列平滑法估算和非平滑算法代码）
29 3
|
1天前
|

【国密算法】国密算法在Java中的实践
【国密算法】国密算法在Java中的实践
9 1
|
3天前
|

10 1
|
3天前
|

6 0
|
3天前
|

5 0
|
6天前
|

12 1
|
6天前
|

Java Queue接口及其常用实现类分析
Java Queue接口及其常用实现类分析
14 2
|
6天前
|

Java中的数据结构与算法解析
Java中的数据结构与算法解析
18 5
|
6天前
|

5 0
|
8天前
|
Java
Java字符串转整数的超简单方法！
Java字符串转整数的超简单方法！
20 0

• 机器翻译
• 工业大脑

更多

更多

更多