`
frank-liu
  • 浏览: 1666091 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

leetcode: 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.

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.

原问题链接:https://leetcode.com/problems/reverse-words-in-a-string/

 

问题分析

  这个问题的思路其实比较简单,因为它只是要把一个字符串里所有非空格的词语顺序给倒过来。那么我们就有这么一个基本的思路,首先将这个串按照空格给划分成多个非空的字符串。然后从后往前将字符串加入到一个StringBuilder中。最后去除两头的空格。当然,问题的要求里提到每个词语之间要有一个空格作为间隔,在实现的时候要注意这一点。

  详细的代码实现如下:

 

public class Solution {
    public String reverseWords(String s) {
        String[] words = s.split("\\s+");
        StringBuilder builder = new StringBuilder();
        for(int i = words.length - 1; i >= 0; i--) builder.append(words[i] + " ");
        return builder.toString().trim();
    }
}

 

1
8
分享到:
评论

相关推荐

    Reverse words in a string-leetcode

    Reverse words in a string-leetcode

    leetcode写题闪退-LeetCode:leetcodeOJ

    leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...

    leetcode字符串括号level-leetcode:LeetCode解题记录

    reverseWords 翻转字符串里的单词 simplifyPath 简化路径 restoreIPAddresses 复原IP地址 threeSum 三数之和 search 搜索旋转排序数组 1. 3. 9. 75. 209. 219. 167. 268. 344. 349. 454. 447. 695. 674. string 字符...

    leetcode中国-leetCodeSolution::thumbs_up:leetCode刷题项目

    reverseWords titleToNumber toLowerCase defangIPaddr replaceSpace removeOuterParentheses 复杂题目值得参考 sortString 使用字典排序 sort((a, b) => a.charCodeAt() - b.charCodeAt()) 有参考价值 Maths isPal

    LeetCode最全代码

    190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [C++](./C++/reverse-bits.cpp) [Python](./Python/reverse-bits.py) | _O(1)_ | _O(1)_ | Easy ||| 191 |[Number of 1 Bits]...

    Coding Interview In Java

    2 Reverse Words in a String II 19 3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder II 29 7 Median of Two Sorted Arrays 33 8 Kth Largest Element in an Array ...

    leetcode2sumc-Leetcode-2020:刷刷Leetcode并作纪录

    leetcode 2 sum c Leetcode 练习记录 这个专案主要存放我练习Leetcode有针对难度分类的集合题库(Collection Question) 准备方式 分析tag的热门标签,熟悉各个标签解题的思路(解决该标签全部的easy和medium为主),再...

    leetcode-go:我使用Golang解决LeetCode问题的方法

    goMy solution to LeetCode problems using GolangProblems 题库Array 数组NoTitle题名DifficultyStatus11Container With Most Water盛最多水的容器MediumSolved26Remove Duplicates from Sorted Array删除有序数组...

    leetcode338-coding_notebook:编码_笔记本

    第 338 章概括 [(雅虎)4。 两个排序数组的中位数](Leetcode 问题/数组和字符串/4.median_of_two_sorted_array.md) [(雅虎)13。...String/151.reverse_words_in_a_string.md) [167. Two Sum 2 - In

    LeetCode 151 – 翻转字符串里的单词

    题目描述 151. 翻转字符串里的单词 解法一:(Python) class Solution: def reverseWords(self, s: str) -> str: return " ".join(reversed(s.split())) 解法二:双端队列(C++) ... string reverseWords(strin

    TWDH#Leetcode-From-Zero#13.反转字符串中的单词 III1

    557. 反转字符串中的单词 IIIpublic String reverseWords(String s) {StringBuilder sb = new S

    cpp-算法精粹

    Reverse Nodes in k-Group Copy List with Random Pointer Linked List Cycle Linked List Cycle II Reorder List LRU Cache Palindrome Linked List 字符串 Valid Palindrome Implement strStr() String to Integer...

Global site tag (gtag.js) - Google Analytics