Reverse Words in a String leetcode java
题目:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Clarification:
- 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.
题解:
一个用了java的s.split(" "),按空格分隔。
然后调用了系统函数:Collections.reverse(list);把list顺序调换了。
最后再把结果存成数组即可。
代码如下:
1 public static String reverseWords(String s) {
2 if(s==null||s.length()==0)
3 return s;
4 String [] result = s.split(" ");
5 if(result==null||result.length==0)
6 return "";
7
8 ArrayList<String> list = new ArrayList<String>();
9
for(int i = 0; i<result.length;i++){
if(!result[i].isEmpty())
list.add(result[i]);
}
Collections.reverse(list);
String ans = new String();
for(int i = 0; i<list.size()-1;i++){
ans += list.get(i)+" ";
}
ans +=list.get(list.size()-1);
return ans;
}
Reverse Words in a String leetcode java的更多相关文章
- 151. Reverse Words in a String(java 注意细节处理)
题目:reverse words in a string Given an input string, reverse the string word by word. For example,Giv ...
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- LeetCode算法题-Reverse Vowels of a String(Java实现-四种解法)
这是悦乐书的第206次更新,第218篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第74题(顺位题号是345).编写一个函数,它将一个字符串作为输入,并仅反转一个字符串的 ...
- 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
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- Scramble String leetcode java
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- Interleaving String leetcode java
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given ...
- Reverse Words in a String | LeetCode OJ | C++
我的思路:先读取每一个单词,存放到容器中:读取完毕后,将容器中的单词倒序写入输出中. #include<iostream> #include<string> #include& ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
随机推荐
- bootstrap css-网格系统
前言:第一次记录点东西,只能勉强算是笔记吧.博主自学前端,深知自己水水的,但还是向把自己学到的东西记录下来,这不刚学习了bootstrap的css部分,现在整理出笔记. 1,Bootstrap网格系统 ...
- SPFA算法 O(kE)
主要思想是: 初始时将起点加入队列.每次从队列中取出一个元素,并对所有与它相邻的点进行修改,若某个相邻的点修改成功,则将其入队.直到队列为空时算法结束. 这个算法,简单的说就是队列优化 ...
- [CC-FNCS]Chef and Churu
[CC-FNCS]Chef and Churu 题目大意: 一个长度为\(n(n\le10^5)\)的数列\(A_{1\sim n}\),另有\(n\)个函数,第\(i\)个函数会返回数组中标号在\( ...
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp
B. Dreamoon and WiFi 题目连接: http://www.codeforces.com/contest/476/problem/B Description Dreamoon is s ...
- HDU 4498 Function Curve (自适应simpson)
Function Curve Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)To ...
- 酷播迷你flv,mp4网页视频播放器(CuPlayerMini)V2.2版[经典黑]演示实例
酷播迷你flv,mp4网页视频播放器(CuPlayerMini)V2.2版[经典黑]演示实例 http://www.cuplayer.com/cu/FreeDown/
- setTimeout 的黑魔法 【event loop】
setTimeout,前端工程师必定会打交道的一个函数.它看上去非常的简单,朴实.有着一个很不平凡的名字--定时器.让年少的我天真的以为自己可以操纵未来.却不知朴实之中隐含着惊天大密.我还记得我第一次 ...
- Windows Phone本地数据库(SQLCE):5、[Association]attribute(翻译)(转)
这是“windows phone mango本地数据库(sqlce)”系列短片文章的第五篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...
- iOS内存管理策略和实践
转:http://www.cocoachina.com/applenews/devnews/2013/1126/7418.html 内存管理策略(memory Management Policy) N ...
- insert 语句后面不要跟 where 等条件语句
insert 语句后面不要跟 where 等条件语句: update 才用得到.