/**
* 双指针的简单思路解法
* @param s
* @return
*/
public String reverserString1(String s){
//将字符串 s 变成 字符数组
char[] c = s.toCharArray();
//定义左右指针
int left = 0;
int right = c.length - 1;
while(left <= right){
//进行左右两端的字符交换
char tempC = c[left];
c[left] = c[right];
c[right] = tempC;
left ++;
right --;
}
return new String(c);
}
/**
* 借助栈的特性,先进后出
* @param s
* @return
*/
public String reverserString2(String s){
LinkedList<Character> stack = new LinkedList<>();
char[] c = s.toCharArray();
for(char c1 : c){
stack.push(c1);
}
//定义字符拼接对象 StringBuilder
StringBuilder sb = new StringBuilder();
//只要stack不空,我们就弹出栈顶元素
while(!stack.isEmpty()){
sb.append(stack.pop());
}
return sb.toString();
}
/**
* 借助原生的StringBuilder进行翻转操作
* @param s
* @return
*/
public String reverserString3(String s){
StringBuilder sb = new StringBuilder(s);
sb.reverse();
return sb.toString();
}