Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
这道题很简单,但是我写的第一个版本居然超时了,没有通过,下面这个版本可以通过。
复习一下String和char的转换:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
char[] cha = {'a','b','c'};
String n = String.valueOf(cha);
public class Solution {
public String reverseString(String s) {
char[] chars = new char[s.length()];
int index = 0;
for (int i = s.length() - 1; i >= 0; i--) {
chars[index++] = s.charAt(i);
}
return new String(chars);
}
}