Java字符串反转的10种⽅式⾃定义revser() 传⼊字符串返回字符串要判断实参为null的特殊情况
⽅式⼀双指针 while遍历异或换位
public static String reverse(String s){
if (s==null) return s;
char[] c = s.toCharArray();
int l = 0, r = c.length-1;
while (l < r) {
c[l] ^= c[r];
c[r] ^= c[l];
c[l++] ^= c[r--];
}
return new String(c);
}
双指针
⽅式⼆单指针 for折半遍历临时变量换位
public static String reverse(String s){
if (s==null) return s;
char[] c = s.toCharArray();
for (int i=0; i<c.length>>1; i++){
char temp = c[c.length-1-i];
c[c.length-1-i] = c[i];
c[i] = temp;
}
return new String(c);
}
单指针
⽅式三递归⾃定义recursion()和swap() 加法法则换位符合⾯向对象程序设计
public static String reverse(String s){
if (s==null) return s;
char[] c = s.toCharArray();
int l = 0, r = c.length-1;
recursion(c,l,r);
java数组字符串转数组return new String(c);
}
public static void recursion(char[] c, int l, int r){
if (l>=r) return;
swap(c,l,r);
recursion(c, l+1, r-1);
}
public static void swap(char[] c, int l, int r){
c[l] = (char) ( c[l] + c[r] );
c[r] = (char) ( c[l] - c[r] );
c[l] = (char) ( c[l] - c[r] );
}
递归
⽅式四更符合⾯向过程设计每⼀步都要实现 for只负责遍历两个指针负责寻减法法则换位
public static String reverse(String s){
if (s==null) return s;
char[] c = s.toCharArray();
int l = 0;
int r = c.length-1;
int half = c.length/2;
for (int i=0; i<half; i++) {
c[l] = (char) ( c[l] - c[r] );
c[r] = (char) ( c[r] + c[l] );
c[l] = (char) ( c[r] - c[l] );
l++;
r--;
}
return new String(c);
}
⾯向过程程序设计
⽅式五字符串拼接正常的for遍历索引新的字符拼接在最前⾯
public static String reverse(String s){
if (s==null) return s;
String res = "";
for (int i=0; i<s.length(); i++)
res = s.charAt(i) + res;
/*
for (int i=s.length()-1; i>=0; i--)
res += s.charAt(i);
*/
return res;
}
字符串拼接
⽅式六 StringBuilder或StringBuffer的reverse()
public static String reverse(String s){
if (s==null) return s;
return new StringBuilder(s).reverse().toString();
// 构造⽅法不能接收字符数组 chars[]需要转换成String才可以
/*char[] chars = s.toCharArray();
return new StringBuilder(new String(chars).reverse().toString();
}
StringBuilder
⽅式七临时数组利⽤for循环将原数组中的字符从后往前存放到临时数组中public static String reverse(String s){
if (s == null) return s;
char[] c = s.toCharArray();
char[] res = new char[c.length];
for (int i=0; i<c.length; i++)
res[i] = c[c.length-1-i];
return new String(res);
}
TemporaryArray
⽅式⼋集合⼯具类只接收list 且Arrays.asList接收的基本数据类型需要转为包装类
public static String reverse(String s){
if (s == null) return s;
char[] c = s.toCharArray();
Character[] res = new Character[c.length];
for (int i=0; i<c.length; i++)
res[i] = c[i];
List<Character> list = Arrays.asList(res);
for (int i=0; i<list.size(); i++)
c[i] = (i);
return new String(c);
}
Collections的reverse
⽅式九栈反转栈反转利⽤Stack后进后出的特性第⼀个遍历存⼊到栈中,第⼆个遍历从栈中取出public static String reverse(String s){
if (s == null) return s;
char[] c = s.toCharArray();
Stack<Character> stack = new Stack<>();
for(char temp : c)
stack.push(temp);
for (int i=0; i<c.length; i++)
c[i] = stack.pop();
return new String(c);
}
Stack
⽅式⼗链表的addFirst()实现先进后出或 addLst实现后进先出
public static String reverse(String s){
if (s == null) return s;
char[] c = s.toCharArray();
LinkedList<Character> list = new LinkedList<>();
for(Character t : c)
list.addFirst(t);
for(int j=0; j<c.length; j++)
c[j] = veFirst();
return new String(c);
}
LinkedList
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论