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 ...
随机推荐
- HTML基础单页面总结(基于w3school教程)
学习了一阵http://www.w3school.com.cn网站上的html教程,发现各个知识点比较分散,个人比较倾向于用一页html文档就把所有涉及的基本html标签元素知识点都展示出来的形式.个 ...
- BZOJ5020 [THUWC 2017]在美妙的数学王国中畅游LCT
题意很明显是要用LCT来维护森林 难点在于如何处理函数之间的关系 我们可以根据题目给的提示关于泰勒展开的式子 将三种函数变成泰勒展开的形式 因为$x∈[0,1]$ 所以我们可以将三个函数在$x_0=0 ...
- BZOJ 3339: Rmq Problem 莫队算法
3339: Rmq Problem 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3339 Description n个数,m次询问l,r ...
- javascript 正则限制文本输入框只允许输入数字,简单实现。
<input type="text" id="memberId" lay-verify="title" autocomplete=&q ...
- 谨慎注意WebBrowser控件的DocumentCompleted事件
引言 WebBrowser控件的DocumentCompleted事件一般就被认定为是在页面完全加载完毕后产生,而注释中也是这么写的: 但事实却并非如此. 首先它不一定会在完全加载完毕时才触发,有时就 ...
- .yaml 文件格式简介
命名 YAML 的意思其实是:"Yet Another Markup Language"(仍是一种置标语言)的缩写. 功能 YAML的语法和其他高阶语言类似,并且可以简单表达清单. ...
- Html学习笔记3
1表格的标题和表头: <table> <caption>成绩单</caption> <tr> <th>姓名</th> <t ...
- Revit API改变风管及管件尺寸
start , , )) < , , -)) < , dHeight = ; ConnectorSetIterator csi = fi.MEPModel.Conn ...
- WebLogic使用总结(三)——WebLogic配置JNDI数据源
一.在WebLogic新建针对Oracle数据库的JNDI数据源 进入weblogic管理控制台,此处是远程访问:http://192.168.1.144:7001/console 点击左侧[ 域结构 ...
- Mustache.js语法
看了Mustache的github,学学此中的语法,做个笔记 1.简单的变量调换:{{name}} 1 var data = { "name": "Willy" ...