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".

click to show clarification.

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         
10         for(int i = 0; i<result.length;i++){
11             if(!result[i].isEmpty())
12                 list.add(result[i]);
13         }
14         Collections.reverse(list);
15         
16         String ans = new String();
17         for(int i = 0; i<list.size()-1;i++){
18             ans += list.get(i)+" ";
19         }
20         ans +=list.get(list.size()-1);
21         return ans;
22     }

Reverse Words in a String leetcode java,布布扣,bubuko.com

时间: 2024-10-12 18:59:29

Reverse Words in a String leetcode java的相关文章

Scramble String leetcode java

题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choo

Interleaving String leetcode java

题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 题解: 这道题还是像

reverse words in a string(java 注意一些细节处理)

题目: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". 解析:将字符串中的单词逆序输出 借助一个堆栈,从前向后遍历字符串,遇到空,跳过,直到非空字符,拼接word,等再次遇到空时,得到一个word,加入堆栈, 以此类推,直到

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. click to show clarification. Clari

Reverse Vowels of a String Leetcode

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". Note:The vowels does not include

(Easy) Reverse Vowels of a String - LeetCode

Description: Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello" Output: "holle" Example 2: Input: "leetcode" Output: "leotcede" Note:The vowels does not

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". For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a word?A sequence of n

151. Reverse Words in a String Leetcode Python

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. linear time: 1. go through the

Leetcode557 Reverse Words in a String III Java实现

这次准备试一试split一个String是用split好,还是手动找Index之后Substring好.因为强哥说Leetcode上运行时间不稳定,有时可能速度会差50%之多! 方法1,split: public class ReverseWordsInAStringIII557 { public String reverseWords(String s) { String[] words = getWordsv1(s); StringBuilder result = new StringBui