344. Reverse String
- Total Accepted: 90670
- Total Submissions: 156010
- Difficulty: Easy
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
Subscribe to see which companies asked this question
字符串翻转
直接翻转的话肯定超时,不然这道题就没有存在的意义了,从两边向中间翻转,可以节约一半的时间
public class Problem344 {
public static String reverseString(String s) {
char[] str = s.toCharArray();
int len = s.length();
char strTemp;
for(int i=0;i<len/2;i++){
strTemp = str[i];
str[i] = str[len-1-i];
str[len-1-i] = strTemp;
}
return new String(str);
}
public static void main(String [] args){
System.out.println(reverseString("Heallo"));
}
}