当前位置:网站首页>力扣(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--;
}
}
};
边栏推荐
- Mixed development of QML and QWidget (preliminary exploration)
- [HBZ sharing] how to locate slow queries in cloud database
- Bubble sort
- Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
- coreldraw2022新版本新功能介绍cdr2022
- ETCD数据库源码分析——etcdserver bootstrap初始化存储
- Crawler notes: improve data collection efficiency! Use of proxy pool and thread pool
- 1008 circular right shift of array elements (20 points)
- How does computer nail adjust sound
- Embedded development program framework
猜你喜欢
Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
Distributed transaction solution
Patent | subject classification method based on graph convolution neural network fusion of multiple human brain maps
[Chongqing Guangdong education] engineering fluid mechanics reference materials of southwestjiaotonguniversity
coreldraw2022新版本新功能介绍cdr2022
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
One question per day (Mathematics)
Introduction to hashtable
Fedora/REHL 安装 semanage
随机推荐
Mixed development of QML and QWidget (preliminary exploration)
I'd like to ask about the current MySQL CDC design. In the full volume phase, if a chunk's binlog backfill phase,
BOM - location, history, pop-up box, timing
Luogu deep foundation part 1 Introduction to language Chapter 2 sequential structure programming
P3500 [POI2010]TES-Intelligence Test(二分&离线)
MIT CMS. 300 session 8 – immersion / immersion
Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
2327. Number of people who know secrets (recursive)
How does computer nail adjust sound
P2022 interesting numbers (binary & digit DP)
DMA use of stm32
ue5 小知识 FreezeRendering 查看视锥内渲染的物体
View 工作流程
【HBZ分享】云数据库如何定位慢查询
Fedora/REHL 安装 semanage
E. Best Pair
Solve the compilation problem of "c2001: line breaks in constants"
Slow SQL fetching and analysis of MySQL database
How to realize automatic playback of H5 video
How does vs change the project type?