`
frank-liu
  • 浏览: 1682447 次
  • 性别: 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

    python-leetcode题解之151-Reverse-Words-in-a-String.py

    python python_leetcode题解之151_Reverse_Words_in_a_String.py

    js-leetcode题解之151-reverse-words-in-a-string.js

    javascript js_leetcode题解之151-reverse-words-in-a-string.js

    python-leetcode题解之186-Reverse-Words-in-a-String-II.py

    python python_leetcode题解之186_Reverse_Words_in_a_String_II.py

    leetcode写题闪退-LeetCode:leetcodeOJ

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

    leetcode分类-leetcode:leetcode刷题(中等难度分类)

    字符串处理题目也是LeetCode的重要部分,例如"Reverse Words in a String"(翻转字符串中的单词)和"Valid Palindrome"(验证回文串)。这些题目通常涉及到字符串的遍历、分割、比较以及特殊字符的处理。 四、位...

    leetcode答案-leetcode:我对leetcode151的回答

    LeetCode 151题,全称为"Reverse Words in a String"(反转字符串中的单词),这是一道中等难度的题目。题目要求用户编写一个函数,接收一个只包含空格和字母的字符串作为输入,然后返回这个字符串中单词的顺序被...

    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 字符...

    leetcodemaxpointsonaline-leetcode:https://oj.leetcode.com/的解决方案

    "Reverse Words in a String"(字符串中的回文子串)涉及字符串处理,可以通过双指针或栈来解决。"Evaluate Reverse Polish Notation"(评估反向波兰表示法)需要理解和处理栈结构来计算表达式。"Sort List"(排序...

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

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

    Leetcode book刷题必备

    6. Reverse Words in a String:翻转字符串中的单词顺序。 7. Reverse Words in a String II:翻转字符串中的单词顺序,并且整个字符串也要翻转。 8. String to Integer (atoi):将字符串转换成整数,考虑溢出等问题...

    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删除有序数组...

    算法刷题笔记leetcode/lintcode

    - Reverse Words in a String(反转字符串中的单词) - Valid Palindrome(回文字符串验证) - Longest Palindromic Substring(最长回文子串) - Space Replacement(URL化) - Wildcard Matching(通配符匹配...

    CleanCodeHandbook_v1.0.3

    - Reverse Words in a String(字符串中的单词翻转) - String to Integer (atoi)(字符串转换整数) - Longest Substring Without Repeating Characters(无重复字符的最长子串) 4. Math(数学): 算法题目中...

    常见算法题答案及解析

    6. Reverse Words in a String:给定一个字符串,将其单词反转并保持单词内部顺序不变。 7. Reverse Words in a String II:字符串处理,涉及到对字符串的反转和单词的分割。 8. String to Integer (atoi):将字符串...

    算法-leetcode-剑指offer上的题很多

    - **反转单词(Reverse Words)**: 将字符串中的单词顺序反转。 - **验证回文字符串(Valid Palindrome)**: 判断一个字符串是否是回文。 - **最长回文子串(Longest Palindromic Substring)**: 找出字符串中最长的回文...

    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

Global site tag (gtag.js) - Google Analytics