`
hcx2013
  • 浏览: 82136 次
社区版块
存档分类
最新评论

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.

 

import java.util.ArrayList;
import java.util.Collections;

public class Solution {
    public String reverseWords(String s) {
    	if (s == null || s.length() == 0) {
    		return s;
    	}
    	String[] split = s.split(" ");
    	if (split == null || split.length == 0) {
    		return "";
    	}
    	ArrayList<String> arrayList = new ArrayList<String>();
    	for (int i = 0; i < split.length; i++) {
			if (!split[i].equals("")) {
				arrayList.add(split[i]);
			}
		}
    	Collections.reverse(arrayList);
    	String res = new String();
    	for (int i = 0; i < arrayList.size() - 1; i++) {
			res += arrayList.get(i) + " ";
		}
    	res += arrayList.get(arrayList.size()-1);
    	return res;
    }
}

 

0
1
分享到:
评论

相关推荐

    Reverse words in a string-leetcode

    Reverse words in a string-leetcode

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

    LeetCode最全代码

    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/...

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

    leetcode 2 sum c Leetcode 练习记录 这个专案主要存放我练习Leetcode有针对难度分类的集合题库(Collection ...Reverse Words in a String medium O 这题有点算是easy的程度, 目前解法为O(n^2)的时

    leetcode写题闪退-LeetCode:leetcodeOJ

    Reverse Words in a String C++实现时那个返回值是void也着实让我困惑了好久 Subsets DFS实现,竟然还WA了好几次。 Climbing Stairs 入门级记忆化dp 2014.10.30 Merge Sorted Array 归并排序基础 Remove Duplicates ...

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

    goMy solution to LeetCode problems using GolangProblems 题库Array 数组...Reverse Words in a String翻转字符串里的单词MediumSolvedTwo Pointers 双指针NoTitle题名DifficultyStatus20Valid Parentheses有效的括号...

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

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

    yp2020#yp2020.github.io#58I翻转单词顺序1

    代码:public String reverseWords(String s) {//这里在加入最后一个单词的时候会多加一个空格//这里在加入最后一个单词的时候

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

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

    php字符串按照单词进行反转的方法

    $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('

    ImageMagick图片批量处理

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

    cpp-算法精粹

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

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

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

    一个跨平台的CString源码

    // static_cast&lt;&gt; in a place in which I should have been using // reinterpret_cast&lt;&gt; (the ctor for unsigned char strings). // That's what happens when I don't unit-test properly! // Arnt also noticed ...

    Git-2.21.0-64-bit.zip

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

    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

    Data Structure and Algorithms

    2.1.5 Traversing the list in reverse order . . . . . . . . . . . . . 13 2.2 Doubly Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.2.1 Insertion . . . . . . . . . . . . . . . . . ...

    Senfore_DragDrop_v4.1

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

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

    Microsoft Library MSDN4DOS.zip

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

Global site tag (gtag.js) - Google Analytics