題意
翻轉字符串。
解法
兩個指針,分別在首和尾,都向中間走,直到首超過尾。
class Solution { public String reverseString(String s) { char[] chars = s.toCharArray(); for (int i = 0, j = chars.length - 1; i < j; i++, j--) { char temp = chars[i]; chars[i] = chars[j]; chars[j] = temp; } return new String(chars); } }
Runtime: 2 ms, faster than 79.62% of Java online submissions for Reverse String.