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 ...
随机推荐
- linux中萌翻了的cowsay命令
想要让你的linux萌翻吗? 首先需要安装cowsay软件 命令如下:sudo apt-get update;sudo apt-get install cowsay 然后对其输入命令,默认情况下,动物 ...
- BFC,IFC,GFC,FFC的定义及功能
What's FC?一定不是KFC,FC的全称是:Formatting Contexts,是W3C CSS2.1规范中的一个概念.它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定 ...
- Windows下安装Django及WEB服务启动
如果使用的是 Linux 或 Mac OS X ,系统可能已经预装了 Python .在命令提示符下 (或 OS X 的终端中) 输入python ,如果出现python编辑环境,说明 ...
- iOS-OC-基础-NSDictionary常用方法
/*=============================NSDictionary(不可变字典)的使用=========================*/ //————————————————— ...
- ORACLE SEQUENCE用法 (自增长)
在oracle中sequence就是序号,每次取的时候它会自动增加.sequence与表没有关系. 1.Create Sequence 首先要有CREATE SEQUENCE或者CREATE ...
- FZU1327 优先队列
Problem 1327 Blocks of Stones II Accept: 318 Submit: 881Time Limit: 1000 mSec Memory Limit : 3 ...
- [Leetcode] Two Sum (C++)
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目: Gi ...
- iconv编码转换指令
看到一个不错的指令iconv,可以对文件编码进行转换,记录如下: iconv --list 列出所有支持转换的编码 icon -f code1 -t code2 filename -o newfile ...
- 【面向代码】学习 Deep Learning(三)Convolution Neural Network(CNN)
========================================================================================== 最近一直在看Dee ...
- 【solr基础教程之九】客户端
一.Java Script 1.由于Solr本身可以返回Json格式的结果,而JavaScript对于处理Json数据具有天然的优势,因此使用JavaScript实现Solr客户端是一个很好的选择. ...