Reverse Words in a String——LeetCode
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.
- 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. 
题目大意:给一个String,以空格分隔,以单词为单位反转这个String。
解题思路:用java自带的split,拆成数组,然后组合。。。
    public String reverseWords(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }
        String[] res = s.split(" ");
        if (res.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = res.length - 1; i >= 0; i--) {
            if ("".equals(res[i])) {
                continue;
            }
            sb.append(res[i]).append(" ");
        }
        return sb.substring(0, sb.length() - 1);
    }
Reverse Words in a String——LeetCode的更多相关文章
- 345. Reverse Vowels of a String - LeetCode
		
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
 - Reverse Words in a String leetcode
		
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
 - Reverse Words in a String leetcode java
		
题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...
 - Reverse Words in a String  |  LeetCode OJ  | C++
		
我的思路:先读取每一个单词,存放到容器中:读取完毕后,将容器中的单词倒序写入输出中. #include<iostream> #include<string> #include& ...
 - [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
		
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
 - [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
		
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
 - [LeetCode] Reverse Words in a String 翻转字符串中的单词
		
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
 - LeetCode Reverse Words in a String II
		
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
 - LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
		
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
 
随机推荐
- codevs1380 没有丧尸的舞会
			
/* 树形DP 而然我并不知道树在哪(....) f[x][0]表示x节点不参加舞会 以x为根的子树的最优解 f[x][1]表示x节点参加舞会 以x为根的子树的最优解 方程为:(so为x的儿子 so要 ...
 - 玩javaweb的web.xml编译路径
			
有时候能够碰到这样的情况 缓存就是 清不掉 那就可以去寻找编译路径了 <Context docBase="E:\java-workspace\eigyo_com405" pa ...
 - PHP 通过随机数获得ASCII 值返回字符。
			
for($i=0;$i<20;$i++){ $Matrix .= chr(65 + rand(0,26)); }
 - IOS开发效率之为Xcode添加常用的代码片段
			
IOS开发效率之为Xcode添加常用的代码片段 原文地址:http://blog.csdn.net/pingchangtan367/article/details/30041285 tableview ...
 - ubuntu libreOffice设置为中文界面
			
在终端输入: sudo apt-get install libreoffice-l10n-zh-cn libreoffice-help-zh-cn 上面的命令是下载中文包,并安装,再次打开libreo ...
 - 十四、C# 支持标准查询运算符的集合接口
			
支持标准查询运算符的集合接口. System.Linq.Enumeralbe类提供的一些常用的API 来执行集合处理 1.匿名类型 2.隐匿类型的局部变量 3.集合初始化器 4.集合 5.标准查询运算 ...
 - 【转】jquery两稳定版本比较~~
			
博客分类: Web前端 jquery jquery历经了多个版本的更新,版本上的比较貌似没什么必要性,一般来说新的版本会比旧的版本各方面都略有提升,但由于新版中增加了各种新的功能,难免会引起bug的 ...
 - 根据CreateDirectory递归创建多级目录
			
分为MFC下的和非MFC下的两种,MFC路径是CString类型的,非MFC的路径是wstring类型的. 下面是MFC下的创建目录: void __fastcall RecursiveDirecto ...
 - illegal mix of collcations表连接时非法的校对
			
背景:旧表导入新表,新表里的字段是字符串类型 新表是int类型 两个字段通过字符串处理后相等 (准备left join 关联起来)报错 把int类型字段更改成varchar字符串类型后成功
 - bzoj2260: 商店购物 && 4349: 最小树形图
			
Description Grant是一个个体户老板,他经营的小店因为其丰富的优惠方案深受附近居民的青睐,生意红火.小店的优惠方案十分简单有趣.Grant规定:在一次消费过程中,如果您在本店购买了精制油 ...