首页 > 代码库 > LeetCode Reverse String

LeetCode Reverse String

原题链接在这里: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