当前位置:网站首页>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();
}
}
边栏推荐
- 变分(Calculus of variations)的概念及运算规则
- Polymorphism and interface
- Number of high-quality number pairs [bit operation characteristics + abstract ability evaluation + grouping fast statistics]
- [eloquence] negotiation persuasion skills and Strategies
- Several methods of spark parameter configuration
- SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你
- jqgrid全选取消单行点击取消事件
- GameFramework制作游戏(二)制作UI界面
- Gonzalez Digital Image Processing Chapter 1 Introduction
- Realsense ROS installation configuration introduction and problem solving
猜你喜欢

Development of uni app offline ID card identification plug-in based on paddleocr

Alibaba cloud installs mysql5.7

43 盒子模型

Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%

27 classification of selectors

41 图片背景综合-五彩导航图

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

阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践

Products on Apple's official website can save 600 yuan by buying iPhone 13 Pro max at a discount

河源市区推出消防安全主题奶茶 助推夏季火灾防控
随机推荐
filters获取data中的数据;filters使用data中的数据
【MySQL必知必会】触发器 | 权限管理
Gameframework making games (I)
GameFramework制作游戏(二)制作UI界面
Development of uni app offline ID card identification plug-in based on paddleocr
Gson and fastjson
[C题目]力扣88. 合并两个有序数组
thymeleaf通过样式控制display是否显示
About RDBMS and non RDBMS [database system]
EDA chip design solution based on AMD epyc server
37 元素模式(行内元素,块元素,行内块元素)
MySQL 45讲 | 06 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
Jqgrid select all cancel single line click cancel event
English grammar_ Indefinite pronoun - other / other
easygui使用的语法总结
Thymeleaf setting disabled
微信公众号正式环境上线部署,第三方公众平台接入
C language and SQL Server database technology
河源市区推出消防安全主题奶茶 助推夏季火灾防控
Overview of cloud security technology development