LeetCode之Reverse String
生活随笔
收集整理的這篇文章主要介紹了
LeetCode之Reverse String
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、題目:
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
2、代碼實現:
代碼實現1:
public static String reverseString(String s) {if (s == null) {return null;}if (s.length() == 0) {return "";}int length = s.length();char[] chars = s.toCharArray();String result = "";for (int i = chars.length - 1; i >=0; --i) {result += chars[i];}return result;} ? 代碼實現2: public String reverseString(String s) {return new StringBuffer(s).reverse().toString();}
?
?
總結
以上是生活随笔為你收集整理的LeetCode之Reverse String的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode之Palindrome
- 下一篇: LeetCode之Hamming Dis