资讯专栏INFORMATION COLUMN

leetcode 151. Reverse Words in a String

yzd / 3245人阅读

摘要:题目要求讲一个字符串中的单词逆序输出,单词中字母顺序不发生改变。其中,字符串首位的空格应删去,字符串中如果存在多余的空格,只需输出一个空格。这里用到的正则表达式为也就是遇到一个或多个空白时断句。

题目要求
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.

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.

讲一个字符串中的单词逆序输出,单词中字母顺序不发生改变。其中,字符串首位的空格应删去,字符串中如果存在多余的空格,只需输出一个空格。

思路一:正则表达式

关于正则表达式的详细信息,请参考我的这篇博客
在这里,我们只需要将句子中的单词通过split的正则表达式读取出来即可。这里用到的正则表达式为s+,也就是遇到一个或多个空白时断句。

    public String reverseWords(String s) {
        String[] array = s.trim().split("s+");
        String result = "";
        for(int i = array.length-1 ; i>0 ; i--){
            result += array[i] + " ";
        }
        return result + array[0];
    }
思路二:双指针

其实双指针这里也用了String的API,不过核心的思路还是在于通过双指针找到一个单词的位置,然后通过获得子字符串的方法将其提取出来加入到结果集中。

    public String reverseWords(String s){
        String trimmed = s.trim();
        int prev = trimmed.length();
        int index = prev;
        StringBuilder result = new StringBuilder();
        while ((index = trimmed.lastIndexOf(" ", index-1)) > 0) {
            if (index < prev-1) {
                result.append(trimmed.substring(index+1, prev));
                result.append(" ");
            }
            prev = index;
        }
        result.append(trimmed.substring(index+1, prev));
        return result.toString();
    }


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70218.html

相关文章

  • LeetCode 151:给定一个字符串,逐个翻转字符串中的每个单词 Reverse Words i

    摘要:说明无空格字符构成一个单词。输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。我们将字符串转为字符型数组并用两个指针来解这道题。指针作为原字符串转为字符数组的索引,从右向左移。 公众号:爱写bug(ID:icodebugs) 翻转字符串里的单词 Given an input string, reverse the string word by word. 示例 1:...

    red_bricks 评论0 收藏0
  • LeetCode 151:给定一个字符串,逐个翻转字符串中的每个单词 Reverse Words i

    摘要:说明无空格字符构成一个单词。输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。我们将字符串转为字符型数组并用两个指针来解这道题。指针作为原字符串转为字符数组的索引,从右向左移。 公众号:爱写bug(ID:icodebugs) 翻转字符串里的单词 Given an input string, reverse the string word by word. 示例 1:...

    dongxiawu 评论0 收藏0
  • LeetCode 之 JavaScript 解答第151题 —— 反转字符串中的单词

    摘要:小鹿题目翻转字符串里的单词给定一个字符串,逐个翻转字符串中的每个单词。说明无空格字符构成一个单词。遇到空格之后,将单词进行倒序拼接。消除尾部的空格。测试用例空字符串。中间空格大于的字符串。 Time:2019/4/20Title: Reverse Words In a StringDifficulty: MidumnAuthor: 小鹿 题目:Reverse Words In a ...

    betacat 评论0 收藏0
  • [Leetcode] Reverse Words in a String 反转单词顺序

    摘要:代码先反转整个数组反转每个单词双指针交换法复杂度时间空间思路这题就是版的做法了,先反转整个数组,再对每个词反转。 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...

    codeKK 评论0 收藏0
  • [LeetCode] Reverse Words in a String II

    Problem Reverse Words in a String IIGiven an input string , reverse the string word by word. Example Input: [t,h,e, ,s,k,y, ,i,s, ,b,l,u,e]Output: [b,l,u,e, ,i,s, ,s,k,y, ,t,h,e] Note A word is defin...

    浠ラ箍 评论0 收藏0

发表评论

0条评论

yzd

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<