LeetCode Reverse String

Posted Dylan_Java_NYC

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 344 - Reverse String

LeetCode(344) Reverse String

LeetCode: 344 Reverse String

Leetcode 151. Reverse Words in a String

LeetCode解题思路:344. Reverse String

[LeetCode] 344 Reverse String & 541 Reverse String II