Leetcode解题笔记-Reverse Words in a String

题目要求:

给定一个字符串由不同单词组成,返回其相反顺序,中间可能有多余字符:

例如:

Given s = "the sky is blue",
return "blue is sky the".

个人解法:

1.暴力,主要是对于两个单词中间存在多个空格的处理。

2. 利用栈来存储临时变量。

3.缺点,空间利用太大

代码:

public String reverseWords(String s) {
Stack<String> stack = new Stack<String>();
String tmp = "";
s= s.trim();
for(int i=0; i<s.length();i++){
if(s.charAt(i)!=‘ ‘){
tmp = tmp+s.charAt(i);
}else{
if(s.charAt(i+1)==‘ ‘){
continue;
}else{
stack.push(tmp);
tmp = "";
}
}
}
if(tmp!=""){
stack.push(tmp);
}
String re="";
while(!stack.empty()){
re += stack.pop()+" ";
}
return re.trim();

}

参考答案分析:

1.利用正则表达式去分解原始字符串:

2. 从后向前遍历加入结果数组当中。

代码:

public static String reverseString(String s){
String[] parts = s.trim().split("\\s+");
String out ="";
if(s.length()>0){
for(int i=parts.length-1; i>0 ; i--){
out+= parts[i]+" ";
}
out +=parts[0];
}
return out;
}

时间: 2024-12-31 10:11:22

Leetcode解题笔记-Reverse Words in a String的相关文章

Leetcode 151题 Reverse Words in a String

时间:2014.05.10 地点:基地 心情:准备刷下Leetcode题了,就从第151题开始吧 ------------------------------------------------------------------------------------------ 一.题目 Reverse Words in a String Given an input string, reverse the string word by word. For example, Given s =

【leetcode】557. Reverse Words in a String III

Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace

[LeetCode][JavaScript][Python]Reverse Vowels of a String

Reverse Vowels of a String Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 将字符串中

【LeetCode】345. Reverse Vowels of a String 解题小结

题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 应该算不上有难度. class Solution { p

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: Given s = "hello", return "holle". Example 2: Given s = "leetcode", return "leotcede". 题目解答: 要求将字符串中所有的元音字母逆转,辅音字

&lt;LeetCode OJ&gt; 345. Reverse Vowels of a String

Total Accepted: 537 Total Submissions: 1488 Difficulty: Easy Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode"

Leetcode解题笔记,basic calculator 1&amp;&amp;basic calculator 2

Basic Calculator1 题目内容: 实现一个带有加减以及括号功能的小计算器,其中输入的没用负数,而且输入的内容也全部是合法表达式. 个人分析: 1.利用stack解题,将得出的临时结果放入stack中 2.遇到括号的时候将result放入stack中 心得: 1. 对于加减可以直接利用符号进行操作,专门设置一个sign的变量去存这个符号 2.转化字符串中数字固定方法:number = number*10+s.charAt(i)-'0' 3. 对于这类需要对中间过程进行记录的可以利用s

LeetCode解题笔记 - 20. Valid Parentheses

这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶段性查看,一定能对自己有帮助. 这是我做的第一题,所以记录也从这题开始,之后尽力以简短的说明,描述出思路,方便以后能回顾到简介明了的记录. 20. Valid Parentheses Given a string containing just the characters '(', ')', '{

LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.