当前位置:网站首页>颠倒字符串中的单词(split、双端队列)
颠倒字符串中的单词(split、双端队列)
2022-06-12 01:38:00 【学海无涯苦作舟呀】
题目链接:https://leetcode.cn/problems/reverse-words-in-a-string/
给你一个字符串 s ,颠倒字符串中 单词 的顺序。
单词 是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的 单词 分隔开。
返回 单词 顺序颠倒且 单词 之间用单个空格连接的结果字符串。
注意:输入字符串 s中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。
解题思路:
1.用split分割,reverse反转,join拼接三个Java内置函数解决
缺点:面试时这么写,面试官可能就会说:面试就先到这吧(狗头)。
调用函数不是本题的初衷。
代码如下:
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) {
//除去开头和末尾空白字符
s = s.trim();
String[] words = s.split(" +");
Collections.reverse(Arrays.asList(words));
return String.join(" ", words);
}
}
用双端队列,由于双端队列支持从队列头部插入的方法,因此我们可以沿着字符串一个一个单词处理,然后将单词压入队列的头部,再将队列转成字符串即可。
代码如下:
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;
//去除字符串开头的空白字符
while (left <= right && s.charAt(left) == ' '){
left++;
}
//去除字符串尾部的空白字符
while (left <= right && s.charAt(right) == ' '){
right--;
}
Deque<String> d = new ArrayDeque<String>();//双端队列
StringBuilder word = new StringBuilder();
while (left <= right) {
char c = s.charAt(left);
if ((word.length() != 0) && (c == ' ')) {
// 将单词压到队列的头部
d.offerFirst(word.toString());
word.setLength(0);//word置空
} else if (c != ' ') {
//考虑字符串内部有空格的情况
word.append(c);
}
++left;
}
d.offerFirst(word.toString());
return String.join(" ", d);
}
}
边栏推荐
- The 14th five year plan and investment feasibility study report of global and Chinese hydraulic ester base oil industry 2022 ~ 2028
- Colorize Voronoi Diagram Template
- 【科普视频】到底什么是透镜天线?
- Unity顶点动画的阴影实现
- Point cloud perception algorithm interview knowledge points (II)
- 2022-06-11: note that in this document, graph is not the meaning of adjacency matrix, but a bipartite graph. In the adjacency matrix with length N, there are n points. Matrix[i][j] represents the dist
- “还是学一门技术更保险!”杭州校区小哥哥转行软件测试,喜提10K+双休!
- Article 6: Design of multi-functional intelligent trunk following control system | undergraduate graduation design - [Key Technology - positioning technology related data (UWB WiFi Bluetooth)]
- 2022工具钳工(高级)复训题库及在线模拟考试题库来源:安全生产模拟考试一点通公众号
- MP3 to Wav to Midi
猜你喜欢

西南林业大学“西林链”通过工信部电子标准院功能测试 | FISCO BCOS案例

A summary of the interface automation test problems most easily encountered

【项目实训】校验注解

Why should a redis cluster use a reverse proxy? Just read this one

我在某大厂做软件测试工程师的《一天完整工作流程》

Weekly CTF 第一周:神奇的磁带

Don't write about the full screen explosion, try the decorator mode, this is the elegant way!!

The road of global evolution of vivo global mall -- multilingual solution

Annotate your own point cloud dataset with labelcloud open source tool as a tutorial of Kitti annotation format (support PCD and bin point clouds)

Esp8266wifi development board collects temperature and humidity data and uploads them to the Internet of things platform
随机推荐
Machines / scenarios not suitable for webrdp
MP3 to Wav to Midi
Annotate your own point cloud dataset with labelcloud open source tool as a tutorial of Kitti annotation format (support PCD and bin point clouds)
Weibull Distribution韦布尔分布的深入详述(2)参数和公式意义
"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
Three times a day (in serial...)
手写MapReduce程序详细操作步骤
Investment analysis and prospect forecast report of wearable biosensor industry for global and Chinese medical monitoring 2022 ~ 2028
[path of system analysts] summary of real problems of system analysts over the years
Make good use of these 28 tools, and the development efficiency soars
Unity顶点动画的阴影实现
Sharing of Manta network parallel chain solutions by Hufu Research Institute
Common assertions for JMeter interface testing
C dynamically calls the static library generated by go
Equipment encryption of industrial control security
Weibull Distribution韦布尔分布的深入详述(1)原理和公式
我在某大厂做软件测试工程师的《一天完整工作流程》
一看就懂的JMeter操作流程
Data visualization big screen - big screen cloud minimalist user manual
Jmeter接口测试之常用断言