344. Reverse String
Difficulty: Easy
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
代码:
public class Solution {
public String reverseString(String s) {
char[] sArray = s.toCharArray();
int length = sArray.length;
for (int i = 0; i < length/2; i ++) {
char temp = sArray[i];
int position = length - 1 - i;
sArray[i] = sArray[position];
sArray[position] = temp;
}
return new String(sArray);
}
}
总觉得这个代码效率有点低,如果有更快的解决方法,欢迎交流啊。