[Leetcode][JAVA] 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".

Clarification:

What constitutes a word?

    A sequence of non-space characters constitutes a word.

Could the input string contain leading or trailing spaces?

    Yes. However, your reversed string should not contain leading or trailing spaces.

How about multiple spaces between two words?

    Reduce them to a single space in the reversed string.

用JAVA或Python有简便方法:

这是我最开始AC的代码,用split()方法:

public String reverseWords(String s) {
        String[] token = s.split(" ");
        String re = "";
        for(int i=token.length;i>0;i--)
        {
            if(token[i-1].compareTo("")!=0)
                re = re + token[i-1] + " ";
        }
        return re.trim();
    }

后来看discuss说是cheat,就改成:

public String reverseWords(String s) {
        ArrayList<String> ls = new ArrayList<String>();
        int i=0;
        while(i<s.length())
        {
            if(!(s.charAt(i)==‘ ‘))
            {
                int j=i;
                String temp = "";
                while(j<s.length() && !(s.charAt(j)==‘ ‘))
                {
                    temp+=s.charAt(j);
                    j++;
                }
                ls.add(temp);
                i=j;
            }
            else
                i++;
        }

        String re = "";
        for(int j=ls.size()-1;j>=0;j--)
        {
            re = re + ls.get(j) + " ";
        }
        return re.equals("")?re:re.substring(0,re.length()-1);
    }

遇到非空格就往下扫描,直到遇到空格或扫描至字符串末尾,则可以获得一个完整的词。
拿一个数据结构(这里用ArrayList)保存截取下来的词,然后从后往前输出。

最后需要把可能存在的末尾空格去掉。

时间: 2024-11-09 08:58:53

[Leetcode][JAVA] Reverse Words in a String的相关文章

[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. Clarification: What constitutes a

【leetcode】Reverse Words in a String

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

【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

LeetCode OJ1: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". 解题思路: 先利用split()方法将句子按空格分为几个单词的列表, 然后reverse()函数使列表逆序, 最后join函数以空格为间隔拼成字符串返回结果. Python代码: class Solution:  

LeetCode OJ - 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". 解题思路: 1.先对字符串进行一次总的翻转 2.以空格为单位,划分单词,然后对每个单词再进行一次翻转 代码: class Solution { public: void swap(char& a, char

Java for 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". 解题思路: 本题方法多多,最简单的方式直接按“ ” spilt即可,JAVA实现如下: public String reverseWords(String s) { if (s == null || s.length()

[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#557. Reverse Words in a String III(反转字符串中的单词 III)

题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 思路 分割字符串,再逆序,拼接到字符串 代码实现 package String; /** * 557. Reverse Words in a St

leetCode 151. Reverse Words in a String 字符串反转 | Medium

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". 题目大意: 输入一个字符串,将单词序列反转. 思路1: 采用一个vector,来存放中间结果 将vector的结果倒序放入原字符串中. 思路2: 在字符串分割的时候