当前位置:网站首页>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);
}
}
边栏推荐
- Dataset how to use dataset gracefully. After reading this article, you can fully understand the dataset in c7n/choerodon/ toothfish UI
- In 2022, the internal promotion of the "MIHA Tour" golden, silver and silver social recruitment started in April and march! Less overtime, good welfare, 200+ posts for you to choose, come and see!
- Simulated 100 questions and simulated examination for safety management personnel of metal and nonmetal mines (small open pit quarries) in 2022
- "C'est plus sûr d'apprendre une technologie!" Hangzhou Campus Little Brother transfer software test, Hi - Ti 10K + double break!
- 【科普视频】到底什么是透镜天线?
- Investment analysis and prospect forecast report of wearable biosensor industry for global and Chinese medical monitoring 2022 ~ 2028
- LeetCode LCP 07. 传递信息
- 华为联运游戏或应用审核驳回:应用检测到支付serviceCatalog:X6
- Make good use of these 28 tools, and the development efficiency soars
- Global and Chinese medical styrene block copolymer industry prospect research and investment planning proposal report 2022-2028
猜你喜欢

Introduction to prism framework - Modular introduction

如何为Excel中的单元格自动填充颜色

In depth description of Weibull distribution (1) principle and formula

【项目实训】微信公众号模板消息推送

初探性能优化!从2个月到4小时的性能提升!

Dataset how to use dataset gracefully. After reading this article, you can fully understand the dataset in c7n/choerodon/ toothfish UI

Basic use of MATLAB

MATLAB basic application 02 wind stock data introduction and use case:

pip运行报错:Fatal error in launcher: Unable to create process using

Entity class dto to VO is converted through plug-in
随机推荐
螺旋矩阵(技巧)
【项目实训】校验注解
Blog recommended | bookkeeper - Apache pulsar high availability / strong consistency / low latency storage implementation
Peewee module basic use -orm
Unit tests in golang
2022年金属非金属矿山(小型露天采石场)安全管理人员考试模拟100题及模拟考试
“還是學一門技術更保險!”杭州校區小哥哥轉行軟件測試,喜提10K+雙休!
Yixin Huachen talks about how to do a good job in customer master data management
Tiobe - programming language ranking in June 2022
[n32g457] remote monitoring of graffiti cloud based on RT thread and n32g457
Detailed explanation and examples of common parameters of curl
In 2022, the internal promotion of the "MIHA Tour" golden, silver and silver social recruitment started in April and march! Less overtime, good welfare, 200+ posts for you to choose, come and see!
Set up NFT blind box mall system | customized development of NFT mall software
Loop loop and CX
The CSV used for JMeter performance test is bullshit
西南林业大学“西林链”通过工信部电子标准院功能测试 | FISCO BCOS案例
MP3 to Wav to Midi
"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
JMeter operation process that can be understood at a glance
Recursive and non recursive transformation