Leetcode in Java && Oracle
424 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2023-06-05_11-03-29.png
44.9 KB
Runtime8 msBeats60.13%
Memory42.3 MBBeats87.73%
#N151. Reverse Words in a String
problem link
#solution
class Solution {
public String reverseWords(String s) {
String[] arr = s.split("\\s+");
for(int i = 0, j = arr.length-1; i<j; ){
if(arr[i].equals("")){
i++; continue;
}
if(arr[j].equals("")){
j--; continue;
}
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++; j--;
}
StringBuilder sb = new StringBuilder();
for(String ss: arr) {
if(!ss.equals("")) sb.append(ss).append(" ");
}
return sb.toString().trim();
}
}
👍2🔥1