LeetCode(69)-Reverse String

题目:

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

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

思路:

  • 题意:反转字符串
  • 不用考虑为空的情况,这种情况sb.toString()也是null,倒叙遍历,StringBuffer相加

代码:

public class Solution {
    public String reverseString(String s) {
        StringBuffer sb = new StringBuffer();
        int n = s.length();
        for(int i = n-1;i >= 0;i--){
            sb.append(s.charAt(i));
        }
        return sb.toString();
    }
}
时间: 2024-10-11 23:04:12

LeetCode(69)-Reverse String的相关文章

[LeetCode] 344 Reverse String & 541 Reverse String II

原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse String II: https://leetcode.com/problems/reverse-string-ii/description/ 题目&解法: 1.Reverse String: Write a function that takes a string as input and returns

leetCode 344. Reverse String 字符串

344. Reverse String Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 思路1: 使用一个新的string来存放结果. class Solution { public:     string reverseString(string s) {        

【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". 思路: easy 算法: public String reverseString(String s) { char

leetcode --344. Reverse String

Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 1 class Solution { 2 public: 3 string reverseString(string s) { 4 int i = 0 ; 5 int j = s.size()-1; 6 while(i<

Python [Leetcode 344]Reverse String

题目描述: Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 解题思路: 见代码. 代码如下: class Solution(object): def reverseString(self, s): """ :type s: str :rtype

[LeetCode]151.Reverse Words in a String

题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. click to show clarification.

【leetcode】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();

LeetCode 557. Reverse Words in a String III

晚饭后写了一题.当然,我这种菜鸟是从easy开始写的...发现leetcode也是有bug的 Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" O

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com