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 ...
随机推荐
- [转帖]音响及DarBee
红外与蓝牙的差别 1.距离 红外:对准.直接.1—2米,单对单 红外线可以用你的手机摄像头看到 蓝牙:10米左右,可加强信号,可以绕弯,可以不对准,可以不在同一间房间,链接最大数目可达7个,同时区分 ...
- SQL Server Management Studio的对象资源管理器的使用
1.查看 2.对象资源管理器 3.点到某个表的身上 4.出现以下图片,因为有时动态创建的触发器,刷新表下面的触发器可能不出来,所以来这里面找
- Android手机修改Hosts的方法
Android手机是和Google帐号紧密联系的,由于中国的操蛋情况,很多时候Google帐号无法登录,导致Android市场无法使用. 在电脑上我们通过修改Hosts方法可以解决Google帐号的登 ...
- SOAPUI请求及mockservice 使用
1.新建soap Project,输入wsdl的地址,运行request 2 ...
- 【POJ2761】【fhq treap】A Simple Problem with Integers
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- PAT - 基础 - 最大公约数和最小公倍数
题目: 本题要求两个给定正整数的最大公约数和最小公倍数. 输入格式: 输入在一行中给出2个正整数M和N(<=1000). 输出格式: 在一行中顺序输出M和N的最大公约数和最小公倍数,两数字间以1 ...
- PHP随机生成指定时间段的指定个数时间
/** * 生成某个范围内的随机时间 * @param <type> $begintime 起始时间 格式为 Y-m-d H:i:s * @param <type> $endt ...
- bootstrap日期时间插件datetimepicker
<!DOCTYPE HTML> 02 <html> 03 <head> 04 <link href="http://netdna.boo ...
- Django db relationship
# coding=utf-8 from django.db import models """ Django数据库关系: 一对一关系:OneToOneField 多对多关 ...
- bzoj2180: 最小直径生成树
Description 输入一个无向图G=(V,E),W(a,b)表示边(a,b)之间的长度,求一棵生成树T,使得T的直径最小.树的直径即树的最长链,即树上距离最远的两点之间路径长度. Input 输 ...