当前位置:网站首页>力扣(LeetCode)186. 翻转字符串里的单词 II(2022.07.05)
力扣(LeetCode)186. 翻转字符串里的单词 II(2022.07.05)
2022-07-06 04:37:00 【ChaoYue_miku】
给定一个字符串,逐个翻转字符串中的每个单词。
示例:
输入: [“t”,“h”,“e”," “,“s”,“k”,“y”,” “,“i”,“s”,” “,“b”,“l”,“u”,“e”]
输出: [“b”,“l”,“u”,“e”,” “,“i”,“s”,” “,“s”,“k”,“y”,” ",“t”,“h”,“e”]
注意:
单词的定义是不包含空格的一系列字符
输入字符串中不会包含前置或尾随的空格
单词与单词之间永远是以单个空格隔开的
进阶:使用 O(1) 额外空间复杂度的原地解法。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/reverse-words-in-a-string-ii
方法一:双指针
C++提交内容:
class Solution {
public:
void reverseWords(vector<char>& s) {
int left = 0;
int right = 0;
int len = s.size();
while (right < len) {
if (s[right] == ' ') {
Swap(s, left, right - 1);
right++;
left = right;
} else {
right++;
}
}
Swap(s, left, len - 1);
Swap(s, 0, len - 1);
}
void Swap(vector<char>& s, int left, int right) {
char temp;
while (left < right) {
temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
};
边栏推荐
- C. The Third Problem(找规律)
- [network] channel attention network and spatial attention network
- Embedded development program framework
- BOM - location, history, pop-up box, timing
- 1291_ Add timestamp function in xshell log
- During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
- View workflow
- VNCTF2022 WriteUp
- Recommendation | recommendation of 9 psychotherapy books
- The value of two date types is subtracted and converted to seconds
猜你喜欢
Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
[face recognition series] | realize automatic makeup
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
canal同步mysql数据变化到kafka(centos部署)
R note prophet
Redis - redis in action - redis actual combat - actual combat Chapter 1 - SMS login function based on redis - redis + token shared session application - with code
[tomato assistant installation]
Basic use of MySQL (it is recommended to read and recite the content)
About some basic DP -- those things about coins (the basic introduction of DP)
题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
随机推荐
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
PTA tiantisai l1-078 teacher Ji's return (15 points) detailed explanation
cdc 能全量拉去oracle 表嘛
VPP performance test
Quick sort
Stable Huawei micro certification, stable Huawei cloud database service practice
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
Scala function advanced
8. Static file
Complete list of common functions of turtle module
P3500 [poi2010]tes intelligence test (two points & offline)
HotSpot VM
Case of Jiecode empowerment: professional training, technical support, and multiple measures to promote graduates to build smart campus completion system
Sqlserver query results are not displayed in tabular form. How to modify them
Cross domain and jsonp details
View 工作流程
word封面下划线
CertBot 更新证书失败解决
Basic use of MySQL (it is recommended to read and recite the content)
How does vs change the project type?