首页 > 其他 > 详细

leetcode-344. Reverse String

时间:2017-03-22 01:06:37      阅读:160      评论:0      收藏:0      [点我收藏+]

344. Reverse String

Write a function that takes a string as input and returns the string reversed.

反转string字符串

java代码:

public class Solution {
    public String reverseString(String s) {
        String a="";
        StringBuilder sb=new StringBuilder(a);
        for(int i=0;i<s.length();i++){
            sb.insert(0,s.charAt(i));
        }
        return sb.toString();
    }
}

  第二种解法,更加快速

java代码:

public class Solution {
    public String reverseString(String s) {
        char[] ch=s.toCharArray();
        int i=0;
        int j=s.length()-1;
        char temp;
        while(i<j){
            temp=ch[i];
            ch[i++]=ch[j];
            ch[j--]=temp;
        }
        return new String(ch);
    }
}

  

leetcode-344. Reverse String

原文:http://www.cnblogs.com/lcbg/p/6597545.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!