Difficult:Medium
題目
Given an input string s, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
翻譯
給定一個輸入字符串 s,顛倒單詞的順序。
單詞被定義為一系列非空格字符。 s 中的單詞將至少用一個空格分隔。
以相反的順序返回由單個空格連接的單詞字符串。
請注意, s 可能包含前導或尾隨空格或兩個單詞之間的多個空格。返回的字符串應該只有一個空格分隔單詞。不要包含任何額外的空格。
範例
Example 1:
1 | Input: s = "the sky is blue" |
Example 2:
1 | Input: s = " hello world " |
Example 3:
1 | Input: s = "a good example" |
解題思路
1.設一個儲存陣列arr
2.若非空白時將它加入字串str,若遇到空白時結束並加入arr並將str=” “
3.迴圈結束將arr組合成字串
程式碼
1 | var reverseWords = function (s) { |