344. Reverse String
escription Submission Solutions
- Total Accepted: 134592
- Total Submissions: 232448
- Difficulty: Easy
- Contributors: Admin
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.
Show Similar Problems
Have you met this question in a real interview?
Yes
No
思路:把字符串反转,通过转化为字符数组,然后倒叙输出存储到StringBuilder 中来。
注意:用String类型会遇到超时问题,需要用到StringBuilder
OJ代码:
public class Solution {
public String reverseString(String s) {
char[] str2=s.toCharArray();
StringBuilder str = new StringBuilder();
for(int i=str2.length-1;i>=0;i--){
//System.out.println(str2[i]);
str.append(str2[i]);
}
return str.toString();
}
}