描述
写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串。(字符串长度不超过1000)答案:双指针
import java.util.*; public class Solution { /** * 反转字符串 * @param str string字符串 * @return string字符串 */ public String solve (String str) { int left = 0, right = str.length() - 1; int mid = left + (right - left) >> 1; char[] arr = str.toCharArray(); while(left != right && left <= mid && right >= mid) { arr[left] ^= arr[right]; arr[right] ^= arr[left]; arr[left] ^= arr[right]; left++; right--; } return new String(arr); } }