Leetcode 151. Reverse Words in a String

preview_player
Показать описание
Leetcode 151 Reverse Words in a String, Leetcode 151, Reverse Words in a String, Leetcode Java, Leetcode Java solution, amazon interview questions, leetcode solution, Pratiksha Bakrola

#leetcode, #leetcode151, #leetcodeSolutions, #leetcodeamazoninterviewquestion

Рекомендации по теме
Комментарии
Автор

Just a small improvement: for building the result we can use a StringBuilder instead of string concatenation since String is immutable in Java

BogdanHladiuc
Автор

__Solution with Two-pointer__
o Split the string

o Then initialize two variables; one as start (0) and the other as end( s.length()-1 )

o Swap continuously until the two pointers meet. The left pointer advances by ++ and right pointer by --

Oh and don't forget to add a " " space to each word after swap. Trim at end of loop

escarluka
Автор

Very helpful. Guide it how to divide big problem and develop a logic. Can you please write your source code using C++.

MrJigneshpatel
Автор

class Solution {
public String reverseWords(String s) {

String[] words= s.split("\\s+");
String reversedString="";

for(int i=words.length-1; i>=0;i--)
{

";
}

return reversedString.trim();
}
}

pratikshabakrola