简单易懂版本,利用双指针实现反转字符串
import java.util.*; public class Solution { /** * 反转字符串 * @param str string字符串 * @return string字符串 */ public String solve (String str) { // write code here if(str.length() <= 1) return str; int left = 0; int right = str.length()-1; char[] chars = str.toCharArray(); while(left < right){ char c = chars[right]; chars[right] = chars[left]; chars[left] = c; left++; right--; } return new String(chars); } }