Fork me on GitHub

Reverse String

题意:

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

Example:
Given s = "hello", return "olleh".

就是将一个字符串翻转输出。

 public String reverseString(String s) {
        if(s == null || s.length() < 2)
            return s;
        char[] chars = s.toCharArray();
        for(int i=0; i<chars.length / 2; i++){
            char temp = chars[chars.length - 1 - i];
            chars[chars.length - 1 - i] = chars[i];
            chars[i] = temp;
        }
        return new String(chars);
    }

 

posted @ 2017-02-17 10:00  insaneXs  阅读(144)  评论(0编辑  收藏  举报