344. Reverse String

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

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

链接:https://leetcode.com/problems/reverse-string/?tab=Description

3/8/2017

第4行的语法要注意

1 public class Solution {
2     public String reverseString(String s) {
3         if (s == null || s.length() < 2) return s;
4         char[] c = new char[s.length()];
5         for (int i = 0; i < s.length(); i++) c[i] = s.charAt(s.length() - i - 1);
6         return new String(c);
7     }
8 }

别人有更好的方法可参考:

https://discuss.leetcode.com/topic/43296/java-simple-and-clean-with-explanations-6-solutions/2

posted @ 2017-03-09 02:13  panini  阅读(120)  评论(0编辑  收藏  举报