当前位置:网站首页>Invert words in a string (split, double ended queue)
Invert words in a string (split, double ended queue)
2022-06-12 01:43:00 【There is no end to learning】
Topic link :https://leetcode.cn/problems/reverse-words-in-a-string/
Give you a string s , Invert the string word The order of .
word Is a string of non whitespace characters .s Use at least one space in the string word Separate .
return word Reverse order and word The result string connected with a single space .
Be careful : Input string s There may be leading spaces in 、 Trailing spaces or multiple spaces between words . In the returned result string , Words should be separated by only a single space , And does not contain any additional spaces .
Their thinking :
1. use split Division ,reverse reverse ,join Splice three Java Built in functions solve
shortcoming : It was written in the interview , The interviewer may say : Let's stop here for the interview ( dog's head ).
Calling a function is not the original intention of this topic .
The code is as follows :
import java.util.Arrays;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
String s = "excample a gg";
System.out.println(reverseWords(s));
}
public static String reverseWords(String s) {
// Remove the leading and trailing white space characters
s = s.trim();
String[] words = s.split(" +");
Collections.reverse(Arrays.asList(words));
return String.join(" ", words);
}
}
Double ended queue , Because the double ended queue supports the method of inserting from the queue head , So we can process word by word along the string , Then press the word into the head of the queue , Then turn the queue into a string .
The code is as follows :
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
String s = " example a gg ";
System.out.println(reverseWords(s));
}
public static String reverseWords(String s) {
int left = 0;
int right = s.length()-1;
// Remove the white space at the beginning of the string
while (left <= right && s.charAt(left) == ' '){
left++;
}
// Remove the white space characters at the end of the string
while (left <= right && s.charAt(right) == ' '){
right--;
}
Deque<String> d = new ArrayDeque<String>();// deque
StringBuilder word = new StringBuilder();
while (left <= right) {
char c = s.charAt(left);
if ((word.length() != 0) && (c == ' ')) {
// Press word to the head of the queue
d.offerFirst(word.toString());
word.setLength(0);//word empty
} else if (c != ' ') {
// Consider the case where there are spaces inside the string
word.append(c);
}
++left;
}
d.offerFirst(word.toString());
return String.join(" ", d);
}
}
边栏推荐
- 初探性能优化!从2个月到4小时的性能提升!
- Redis startup and shutdown commands
- Pytorch model loading and saving, and training based on the saved model
- 聯調這夜,我把同事打了...
- These three sentences make you not confused when you understand that you are an we media. I just do this. The monthly income of video has exceeded 10000 yuan
- Watermelon video synchronization Tiktok also has benefits ~ the official "China Video Partner Program"
- 2022年金属非金属矿山(小型露天采石场)安全管理人员考试模拟100题及模拟考试
- [popular science video] what is a lens antenna?
- 实体类DTO转VO通过插件转化
- 【科普视频】到底什么是透镜天线?
猜你喜欢

联调这夜,我把同事打了...

【项目实训】微信公众号获取用户openid

How to access the traifik proxy dashboard using the rancher desktop

MySQL实训报告【带源码】

"It's safer to learn a skill!" The little brother of Hangzhou campus changes to software testing, and likes to mention 10k+ weekend!

Introduction to prism framework - Modular introduction

Huawei, this is too strong

Quatre schémas de mise en file d'attente des messages pour redis

如何让杀毒软件停止屏蔽某个网页?以GDATA为例

Software engineering - system flow chart
随机推荐
Recursive and non recursive transformation
The resignation of the chief oracle engineer was furious: MySQL is a terrible database, so it is recommended to consider PostgreSQL!
Matlab 基础04 - 冒号Colon operator “:”的使用和复杂应用详析
【科普视频】到底什么是透镜天线?
"Xilin chain" of Southwest Forestry University passed the functional test of Electronic Standards Institute of the Ministry of industry and information technology | FISCO bcos case
The annual salary of testers in large factories ranges from 300000 to 8K a month. Roast complained that the salary was too low, but he was ridiculed by netizens?
Shadow implementation of unity vertex animation
华为,这也太强了吧..
Redis startup and shutdown commands
Lua function
Perceptron from 0 to 1
Matlab foundation 04 - detailed analysis of the use and complex application of colon operator ":"
Elementary OJ problem of binary tree
【项目实训】微信公众号模板消息推送
Matlab 基础应用02 wind 股票数据介绍和使用案例:
Dataset how to use dataset gracefully. After reading this article, you can fully understand the dataset in c7n/choerodon/ toothfish UI
Vue3+ts+node creates personal blog (database design)
TIOBE - 2022年6月编程语言排行
PCA from 0 to 1
如何为Excel中的单元格自动填充颜色