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".
中文:给定一个输入字符串,依照单词逆转之。
比如:
给定 s="the sky is blue",
返回 "blue is sky the"
说明:什么组成一个单词?
一个非空的字符序列组成一个单词。
输入的字符串能够包括头或尾的空格吗?
能够。可是你的倒转的字符串不应该包括头尾空格。
两个单词间有多个空格又如何呢?
在倒转的字符串中把它们降低为一个空格。
此题比較简单,主要考虑使用空格进行切割和空格的去除。
Java:
public String reverseWords(String s) {
if(s.trim() == "")
return null;
String str[] = s.trim().split("\\s+");
StringBuilder sb = new StringBuilder();
int len = str.length;
for(int i=len-1;i>=0;i--){
if(str[i] == " ")
continue;
sb.append(str[i]+" ");
}
return sb.toString().trim();
}
LeetCode——Reverse Words in a String的更多相关文章
- 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 ...
- [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 III 翻转字符串中的单词之三
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- LeetCode: Reverse Words in a String 解题报告
Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...
- LeetCode Reverse Vowels of a String
原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...
- LeetCode: Reverse Words in a String && Rotate Array
Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...
- [leetcode]Reverse Words in a String @ Python
原题地址:https://oj.leetcode.com/problems/reverse-words-in-a-string/ 题意: Given an input string, reverse ...
随机推荐
- 批量创建prefab
using UnityEngine; using System.Collections; using UnityEngine.UI; using System.IO; using UnityEdito ...
- SQLLoader1(简单测试,以控制文件方式导入数据)
1.创建表:SQL> conn scott/tiger@orcl;已连接. SQL> CREATE TABLE EMP2 AS SELECT * FROM EMP WHERE 1=2; 表 ...
- 面试总结之html+css
最近面试了一些公司,和技术总监聊了一些前端技术方面的内容.回来之后我总结了一下,大致可以分为三个模块:第一.Html与css 方面:第二.浏览器解析方面:第三.js方面.打算,分为三篇博文,根据自己的 ...
- NFinal 视图—用户控件
自定义控件 定义控件 以Label控件为例: 1.首先在Common文件夹下添加Label.cs文件,其中代码如下: //a.control的实体类必须继承NFinal.UserControl类 pu ...
- C#中静态类、静态方法和静态变量的简单说明
静态方法与静态变量一样,属于类本身,而不属于哪个类的一个对象.调用一个被定义为static的方法,只有通过在它前面加上这个类的名称.一般定义里说是可以通过实例调用的.其他语言我还没测试,但是在C#中是 ...
- A Bit Of Knowledge
iOS推崇使用png格式的图片,说这样不会失帧 imageNamed 和 imageWithContentOfFile的区别 imageNamed会使用系统缓存,对重复加载的图片速度会快一些,效果好. ...
- codeforces 336C Vasily the Bear and Sequence(贪心)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Vasily the Bear and Sequence Vasily the b ...
- Niagara AX之在Station下显示Home节点
默认的Station下是没有Home节点的,那么,这个Home节点是怎么添加上去的呢? 注意Home后面的描述(Description):“Navigation tree defined by nav ...
- android 常用颜色
reference: http://blog.csdn.net/leewenjin/article/details/17386265
- [Head First Python]6. 定制数据对象:打包代码与数据
相同功能,演进实现 数据文件 sarah2.txt sarah Sweeney,2002-6-17,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55 1- 返回 ...