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.
public static String reverseWords(String s) { if(s == null){ return s; }else if(s.trim().indexOf(" ") < 0){ return s.trim(); }else{ String[] words = s.trim().split("\\s+"); String result = ""; for(int i=(words.length - 1); i>=0; i--){ result = result + " " + words[i]; } return result.trim(); } }
相关推荐
Reverse words in a string-leetcode
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 ...
421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...
leetcode 2 sum c Leetcode 练习记录 这个专案主要存放我练习Leetcode有针对难度分类的集合题库(Collection ...Reverse Words in a String medium O 这题有点算是easy的程度, 目前解法为O(n^2)的时
Reverse Words in a String C++实现时那个返回值是void也着实让我困惑了好久 Subsets DFS实现,竟然还WA了好几次。 Climbing Stairs 入门级记忆化dp 2014.10.30 Merge Sorted Array 归并排序基础 Remove Duplicates ...
goMy solution to LeetCode problems using GolangProblems 题库Array 数组...Reverse Words in a String翻转字符串里的单词MediumSolvedTwo Pointers 双指针NoTitle题名DifficultyStatus20Valid Parentheses有效的括号...
557. 反转字符串中的单词 IIIpublic String reverseWords(String s) {StringBuilder sb = new S
代码:public String reverseWords(String s) {//这里在加入最后一个单词的时候会多加一个空格//这里在加入最后一个单词的时候
reverseWords titleToNumber toLowerCase defangIPaddr replaceSpace removeOuterParentheses 复杂题目值得参考 sortString 使用字典排序 sort((a, b) => a.charCodeAt() - b.charCodeAt()) 有参考价值 Maths isPal
$s = Reversing a string by word; // break the string up into words $words = explode(' ',$s); // reverse the array of words $words = array_reverse($words); // rebuild the string $s = implode('
-caption string assign a caption to an image -channel type apply option to select image channels -colors value preferred number of colors in the image -colorspace type alternate image colorspace ...
Kth Smallest Element in a BST 二叉树的递归 Minimum Depth of Binary Tree Maximum Depth of Binary Tree Path Sum Path Sum II Binary Tree Maximum Path Sum Populating Next Right Pointers in Each Node Sum ...
题目描述 151. 翻转字符串里的单词 解法一:(Python) class Solution: def reverseWords(self, s: str) -> str: return " ".join(reversed(s.split())) 解法二:双端队列(C++) ... string reverseWords(strin
// static_cast<> in a place in which I should have been using // reinterpret_cast<> (the ctor for unsigned char strings). // That's what happens when I don't unit-test properly! // Arnt also noticed ...
compute in a way that is compatible with "git patch-id --stable". * The "git log" command by default behaves as if the --mailmap option was given. UI, Workflows & Features * The "git fast-...
第 338 章概括 [(雅虎)4。 两个排序数组的中位数](Leetcode 问题/数组和字符串/4.median_of_two_sorted_array.md) [(雅虎)13。...String/151.reverse_words_in_a_string.md) [167. Two Sum 2 - In
2.1.5 Traversing the list in reverse order . . . . . . . . . . . . . 13 2.2 Doubly Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.2.1 Insertion . . . . . . . . . . . . . . . . . ...
If you mail a bug report to me, please include the words "Drag Drop" in the subject of your email. ------------------------------------------- 7. Upgrades and bug fixes: ----------------------------...
reverseWords 翻转字符串里的单词 simplifyPath 简化路径 restoreIPAddresses 复原IP地址 threeSum 三数之和 search 搜索旋转排序数组 1. 3. 9. 75. 209. 219. 167. 268. 344. 349. 454. 447. 695. 674. string 字符...
Appendix D MASM Reserved Words Appendix E Default Segment Names Appendix F Error Messages Glossary INDEX Microsoft Macro Assembler - White Paper Table of Contents Product Overview The MASM 6.0 ...