当前位置:网站首页>力扣(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(找规律)
- Cross domain and jsonp details
- 2328. Number of incremental paths in the grid graph (memory search)
- Basic use of MySQL (it is recommended to read and recite the content)
- 1008 circular right shift of array elements (20 points)
- How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
- Database - MySQL storage engine (deadlock)
- CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)
- word封面下划线
- Slow SQL fetching and analysis of MySQL database
猜你喜欢

Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Distributed transaction solution

CertBot 更新证书失败解决

满足多元需求:捷码打造3大一站式开发套餐,助力高效开发

RTP GB28181 文件测试工具

Stable Huawei micro certification, stable Huawei cloud database service practice

Cross domain and jsonp details

Meet diverse needs: jetmade creates three one-stop development packages to help efficient development

电脑钉钉怎么调整声音
随机推荐
Sqlserver query results are not displayed in tabular form. How to modify them
Mlapi series - 04 - network variables and network serialization [network synchronization]
VPP performance test
Use sentinel to interface locally
[network] channel attention network and spatial attention network
Stable Huawei micro certification, stable Huawei cloud database service practice
Query the number and size of records in each table in MySQL database
Lombok principle and the pit of ⽤ @data and @builder at the same time
【HBZ分享】云数据库如何定位慢查询
It is also a small summary in learning
SharedPreferences source code analysis
8. Static file
How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
VPP性能测试
POI add border
CADD course learning (8) -- virtual screening of Compound Library
ETCD数据库源码分析——etcdserver bootstrap初始化存储
npm命令--安装依赖包--用法/详解
RTP GB28181 文件测试工具
Sentinel sliding window traffic statistics