码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Reverse String

时间:2016-08-27 06:33:11      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

原题链接在这里:https://leetcode.com/problems/reverse-string/

题目:

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

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

题解:

Reverse string是常见题, string在Java中是primitive类型, 一旦生成不可改变.

AC Java:

 1 public class Solution {
 2     public String reverseString(String s) {
 3         if(s == null || s.length() == 0){
 4             return s;
 5         }
 6         
 7         StringBuilder sb = new StringBuilder(s);
 8         return sb.reverse().toString();
 9     }
10 }

 

LeetCode Reverse String

标签:

原文地址:http://www.cnblogs.com/Dylan-Java-NYC/p/5812194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!