当前位置:网站首页>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();
}
}
边栏推荐
- 二维数组赋初值你会几种方法?
- Resource not found: rgbd_launch 解决方案
- Why do China Construction and China Railway need this certificate? What is the reason?
- 关于ROS2安装connext RMW的进度条卡在13%问题的解决办法
- Examples of bio, NiO, AIO
- 44 Sina navigation, Xiaomi sidebar exercise
- Heyuan City launched fire safety themed milk tea to boost fire prevention and control in summer
- Idea error failed to determine a suitable driver class
- The main function of component procurement system, digital procurement helps component enterprises develop rapidly
- 41 图片背景综合-五彩导航图
猜你喜欢

【MySQL必知必会】触发器 | 权限管理

Ssh server rejected password

PS制作加载GIF图片教程

Go language founder leaves Google

D2. picking carrots (hard version) (one question per day)

河源市区推出消防安全主题奶茶 助推夏季火灾防控

变分(Calculus of variations)的概念及运算规则

Browser based split screen reading

45padding won't open the box
![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]
随机推荐
Quickly set up dobbo demo
冈萨雷斯 数字图像处理 第一章绪论
二维数组赋初值你会几种方法?
SSM framework integration, simple case
阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
基于浏览器的分屏阅读
45padding不会撑开盒子的情况
Gameframework making games (I)
006 operator introduction
直播课堂系统05-后台管理系统
spark参数调整调优
Software testing -- 1. Outline of software testing knowledge
Leetcode-198- house raiding
PHP 通过原生CURL实现非阻塞(并发)请求模式
How many ways can you assign initial values to a two-dimensional array?
C language and SQL Server database technology
优质数对的数目[位运算特点+抽象能力考察+分组快速统计]
Is it safe for Guolian securities to buy shares and open an account?
Gameframework making games (II) making UI interface
Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]