import java.util.*; public class Solution { /** * 反转字符串 * @param str string字符串 * @return string字符串 */ public String solve (String str) { int len = str.length(); char [] res = new char[len]; for(int j=len-1; j>=0; j--){ res[j] = str.charAt(len-1-j); } String str2 = new String(res); return str2; } }
原地对半反转也不错
import java.util.*; public class Solution { /** * 反转字符串 * @param str string字符串 * @return string字符串 */ public String solve (String str) { int len = str.length(); int i=0; int j =len-1; char[] strings = str.toCharArray(); while(i<j){ char temp = strings[i]; strings[i] = strings[j]; strings[j] = temp; i++; j--; } return new String(strings); } }