当前位置:网站首页>LeetCode_字符串_中等_151.颠倒字符串中的单词
LeetCode_字符串_中等_151.颠倒字符串中的单词
2022-07-25 14:44:00 【小城老街】
1.题目
给你一个字符串 s ,颠倒字符串中单词的顺序。
单词是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的单词分隔开。
返回单词顺序颠倒且单词之间用单个空格连接的结果字符串。
注意:输入字符串 s中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。
示例 1:
输入:s = “the sky is blue”
输出:“blue is sky the”
示例 2:
输入:s = " hello world "
输出:“world hello”
解释:颠倒后的字符串中不能存在前导空格和尾随空格。
示例 3:
输入:s = “a good example”
输出:“example good a”
解释:如果两个单词间有多余的空格,颠倒后的字符串需要将单词间的空格减少到仅有一个。
提示:
1 <= s.length <= 104
s 包含英文大小写字母、数字和空格 ’ ’
s 中至少存在一个单词
进阶:如果字符串在你使用的编程语言中是一种可变数据类型,请尝试使用 O(1) 额外空间复杂度的原地解法。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/reverse-words-in-a-string
2.思路
(1)调用 API
(2)手动实现
3.代码实现(Java)
//思路1————调用 API
class Solution {
public String reverseWords(String s) {
//去除开头和末尾的空格
s = s.trim();
//正则匹配连续的空白字符作为分隔符来进行分割
List<String> words = Arrays.asList(s.split("\\s+"));
//翻转 words 中的单词
Collections.reverse(words);
//使用空格将 words 中的单词拼接成一个字符串并返回
return String.join(" ", words);
}
}
//思路2————手动实现
class Solution {
public String reverseWords(String s) {
//每个单词的开始索引和结束索引
int start, end;
StringBuilder builder = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--) {
//遇到空格则跳过
if (s.charAt(i) == ' ') {
continue;
}
//找到结束索引
end = i + 1;
while (i >= 0 && s.charAt(i) != ' ') {
i--;
}
//找到开始索引
start = i + 1;
//截取当前单词并拼接到 builder 中
builder.append(s.substring(start, end));
builder.append(' ');
}
//删除最后一个多余的空格
builder.deleteCharAt(builder.length() - 1);
return builder.toString();
}
}
边栏推荐
- 没错,请求DNS服务器还可以使用UDP协议
- Awk from getting started to digging in (20) awk parsing command line parameters
- 苹果官网产品打折 买iPhone 13 Pro Max 可省600元
- 物理量与单位符号的书写标准
- The concept and operation rules of calculus of variations
- Practical guide for network security emergency response technology (Qianxin)
- 河源市区推出消防安全主题奶茶 助推夏季火灾防控
- IDEA报错 Failed to determine a suitable driver class
- Examples of bio, NiO, AIO
- spark参数调整调优
猜你喜欢

Deng Qinglin, a technical expert of Alibaba cloud: Best Practices for disaster recovery and remote multi activity across availability zones on cloud

45padding won't open the box
![优质数对的数目[位运算特点+抽象能力考察+分组快速统计]](/img/c9/8f8f0934111f7ae8f8abd656d92f12.png)
优质数对的数目[位运算特点+抽象能力考察+分组快速统计]

51单片机学习笔记(2)

44 新浪导航 ,小米边栏 练习
![Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]](/img/b6/62a346174bfa63fe352f9ef7596bfc.png)
Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]

44 Sina navigation, Xiaomi sidebar exercise

Why do China Construction and China Railway need this certificate? What is the reason?

As methods for viewing and excluding dependencies

Wechat official account official environment online deployment, third-party public platform access
随机推荐
关于ROS2安装connext RMW的进度条卡在13%问题的解决办法
Realsense ROS installation configuration introduction and problem solving
45padding不会撑开盒子的情况
thymeleaf设置disabled
Spark parameter adjustment and tuning
[MySQL series] - how much do you know about the index
GameFramework制作游戏(一)
Educational codeforces round 132 (rated for Div. 2) C, d+ac automata
The main function of component procurement system, digital procurement helps component enterprises develop rapidly
Ten common application scenarios of redis
awk从入门到入土(23)awk内置变量ARGC、ARGC--命令行参数传递
Throwing OutOfMemoryError “Could not allocate JNI Env“
27 classification of selectors
Structure size
Resource not found: rgbd_ Launch solution
gson与fastjson
国联证券买股票开户安全吗?
Melody + realsense d435i configuration and error resolution
Educational Codeforces Round 132 (Rated for Div. 2) C,D+AC自动机
Wechat official account official environment online deployment, third-party public platform access