当前位置:网站首页>LeetCode_ String_ Medium_ 151. Reverse the words in the string
LeetCode_ String_ Medium_ 151. Reverse the words in the string
2022-07-25 14:50:00 【Old street of small town】
1. subject
Give you a string s , Reverse the order of words in a string .
A word is a string of non whitespace characters .s Use in At least one space Separate the words in the string .
Return words in reverse order and use Single space Connected result string .
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 .
Example 1:
Input :s = “the sky is blue”
Output :“blue is sky the”
Example 2:
Input :s = " hello world "
Output :“world hello”
explain : The inverted string cannot contain leading and trailing spaces .
Example 3:
Input :s = “a good example”
Output :“example good a”
explain : If there are extra spaces between two words , The inverted string needs to reduce the space between words to only one .
Tips :
1 <= s.length <= 104
s Contains English upper and lower case letters 、 Numbers and spaces ’ ’
s At least one word exists in
Advanced : If the string is a variable data type in the programming language you use , Please try using O(1) Additional space complexity In situ solution .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/reverse-words-in-a-string
2. Ideas
(1) call API
(2) Manual implementation
3. Code implementation (Java)
// Ideas 1———— call API
class Solution {
public String reverseWords(String s) {
// Remove the spaces at the beginning and end
s = s.trim();
// Regular matching continuous white space characters as separators to segment
List<String> words = Arrays.asList(s.split("\\s+"));
// Flip words The words in
Collections.reverse(words);
// Using spaces will words The words in are spliced into a string and returned
return String.join(" ", words);
}
}
// Ideas 2———— Manual implementation
class Solution {
public String reverseWords(String s) {
// Start index and end index of each word
int start, end;
StringBuilder builder = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--) {
// If you encounter spaces, skip
if (s.charAt(i) == ' ') {
continue;
}
// End index found
end = i + 1;
while (i >= 0 && s.charAt(i) != ' ') {
i--;
}
// Find the start index
start = i + 1;
// Intercept the current word and splice it to builder in
builder.append(s.substring(start, end));
builder.append(' ');
}
// Delete the last extra space
builder.deleteCharAt(builder.length() - 1);
return builder.toString();
}
}
边栏推荐
- Gonzalez Digital Image Processing Chapter 1 Introduction
- 39 simple version of millet sidebar exercise
- 45padding won't open the box
- awk从入门到入土(23)awk内置变量ARGC、ARGC--命令行参数传递
- L1和L2正则化
- 37 element mode (inline element, block element, inline block element)
- English grammar_ Indefinite pronoun - other / other
- 44 Sina navigation, Xiaomi sidebar exercise
- I2C device driver hierarchy
- [C题目]力扣88. 合并两个有序数组
猜你喜欢

GameFramework制作游戏(二)制作UI界面

The solution to the problem that the progress bar of ros2 installation connext RMW is stuck at 13%

43 盒子模型

AS查看依赖关系和排除依赖关系的办法

27 classification of selectors

IP address classification, which determines whether a network segment is a subnet supernetwork

Melodic + Realsense D435i 配置及错误问题解决

云安全技术发展综述
![Number of high-quality number pairs [bit operation characteristics + abstract ability evaluation + grouping fast statistics]](/img/c9/8f8f0934111f7ae8f8abd656d92f12.png)
Number of high-quality number pairs [bit operation characteristics + abstract ability evaluation + grouping fast statistics]

Leetcode-198- house raiding
随机推荐
gson与fastjson
Awk from getting started to digging in (23) awk built-in variables argc, argc -- command line parameter transfer
物理量与单位符号的书写标准
06、类神经网络
[nuxt 3] (XI) transmission & module
Polymorphism and interface
51 single chip microcomputer learning notes (1)
Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]
Examples of bio, NiO, AIO
Paddlenlp's UIE relationship extraction model [executive relationship extraction as an example]
转载----如何阅读代码?
51 single chip microcomputer learning notes (2)
Resource not found: rgbd_ Launch solution
直播课堂系统05-后台管理系统
The input input box of H5 page pops up the numeric keypad, which needs to support decimal points
About RDBMS and non RDBMS [database system]
Products on Apple's official website can save 600 yuan by buying iPhone 13 Pro max at a discount
Thymeleaf controls whether display is displayed through style
Kibana operation es
Gameframework making games (II) making UI interface