Leetcode 151 Reverse Words in a String
Given an input string, reverse the string word by word.
Example:
Input: "the sky is blue", Output: "blue is sky the".
Note:
- A word is defined as a sequence of non-space characters.
- Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
- You need to reduce multiple spaces between two words to a single space in the reversed string.
Follow up: For C programmers, try to solve it in-place in O(1) space.
這個題的意思是將一串字元倒置。
(1)我的方法是直接將字串分離出來再進行重新的拼接
public class Solution {
public String reverseWords(String s) {
String[] ss = s.trim().split("\\s+");//trim().split("\\s+")
String a = "";
for(int i = ss.length - 1;i >= 1 ;--i){
a += ss[i] + " ";
}
return a + ss[0];
}
}
(2)最佳的是在O(1)的空間複雜度下完成的
public class Solution {
public String reverseWords(String s) {
StringBuilder sb = new StringBuilder();
int index = s.length() - 1;
while (index >= 0) {
if (s.charAt(index) == ' ') {
index--;
continue;
}
int start = s.lastIndexOf(' ', index) + 1;
sb.append(' ');
sb.append(s.substring(start, index + 1));//將空格兩邊的字元進行重新的拼接
index = start - 1;
}
if (sb.length() > 0) sb.deleteCharAt(0);//這一行的意思是將最前面的空格去掉
return sb.toString();//轉化為String
}
}
相關文章
- leetcode 344. Reverse StringLeetCode
- Reverse Vowels of a String
- Leetcode 7 Reverse IntegerLeetCode
- Leetcode 273 Integer to English WordsLeetCode
- Leetcode 30 Substring with Concatenation of All WordsLeetCode
- Leetcode 206. Reverse Linked ListLeetCode
- LeetCode之Unique Morse Code Words(Kotlin)LeetCodeKotlin
- Reverse a String-freecodecamp演算法題目演算法
- Leetcode 25 Reverse Nodes in k-GroupLeetCode
- LeetCode Reverse Integer(007)解法總結LeetCode
- LeetCode - Easy - 206. Reverse Linked ListLeetCode
- [LeetCode] 884. Uncommon Words from Two SentencesLeetCode
- [LeetCode] Rotate StringLeetCode
- 【Leetcode】25.Reverse Nodes in k-GroupLeetCode
- LeetCode - 解題筆記 - 7 - Reverse IntegerLeetCode筆記
- Leetcode 481 Magical StringLeetCode
- LeetCode-151-翻轉字串裡的單詞LeetCode字串
- [Leetcode力扣 25] Reverse Nodes in k-GroupLeetCode力扣
- Leetcode 8 String to Integer (atoi)LeetCode
- 【Leetcode】767. Reorganize StringLeetCode
- 【Leetcode】1528. Shuffle StringLeetCode
- 【LeetCode】493. Reverse Pairs 翻轉對(Hard)(JAVA)LeetCodeAIJava
- [LeetCode] Short Encoding of Words 單詞集的短編碼LeetCodeEncoding
- [LeetCode] 678. Valid Parenthesis StringLeetCode
- [LeetCode] 844. Backspace String CompareLeetCode
- [LeetCode] 3163. String Compression IIILeetCode
- LeetCode 438. Find All Anagrams in a StringLeetCode
- LeetCode 394. Decode String All In OneLeetCode
- 資料結構與演算法 | Leetcode 206:Reverse Linked List資料結構演算法LeetCode
- LeetCode String to Integer (atoi)(008)解法總結LeetCode
- LeetCode之Construct String from Binary Tree(Kotlin)LeetCodeStructKotlin
- LeetCode C++ 387. First Unique Character in a String【String/Hash Table】簡單LeetCodeC++
- [LeetCode] 1545. Find Kth Bit in Nth Binary StringLeetCode
- [LeetCode] 2825. Make String a Subsequence Using Cyclic IncrementsLeetCodeREM
- JavaScript reverse()JavaScript
- bag-of-words
- [LeetCode] 1750. Minimum Length of String After Deleting Similar EndsLeetCodeMILA
- BUUCTF reverse 3