344. Reverse String OJ代码

来源:互联网 发布:结构化查询语言sql 编辑:程序博客网 时间:2024/05/16 15:46

344. Reverse String

D

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 Tags
Show Similar Problems
Have you met this question in a real interview? 
Yes
 
No

Discuss Pick One


思路:把字符串反转,通过转化为字符数组,然后倒叙输出存储到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();
    }
}


0 0
原创粉丝点击