当前位置:网站首页>力扣(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--;
}
}
};
边栏推荐
- CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)
- Knowledge consolidation source code implementation 3: buffer ringbuffer
- cdc 能全量拉去oracle 表嘛
- MIT CMS. 300 session 8 – immersion / immersion
- Lambda expression learning
- 2327. Number of people who know secrets (recursive)
- 11. Intranet penetration and automatic refresh
- Lombok principle and the pit of ⽤ @data and @builder at the same time
- Data processing methods - smote series and adasyn
- Mlapi series - 04 - network variables and network serialization [network synchronization]
猜你喜欢
Visio draw fan
Redis - redis in action - redis actual combat - actual combat Chapter 1 - SMS login function based on redis - redis + token shared session application - with code
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
Vulnerability discovery - vulnerability probe type utilization and repair of web applications
[detailed steps of FreeRTOS shift value for the first time]
Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill
[network] channel attention network and spatial attention network
Lombok principle and the pit of ⽤ @data and @builder at the same time
Data processing methods - smote series and adasyn
随机推荐
Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
【Try to Hack】john哈希破解工具
Fedora/REHL 安装 semanage
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
DMA use of stm32
2327. 知道秘密的人数(递推)
Solution of storage bar code management system in food industry
E. Best Pair
Lagrange polynomial
Lombok principle and the pit of ⽤ @data and @builder at the same time
SharedPreferences source code analysis
[FreeRTOS interrupt experiment]
Stable Huawei micro certification, stable Huawei cloud database service practice
P3500 [POI2010]TES-Intelligence Test(二分&离线)
coreldraw2022新版本新功能介绍cdr2022
canal同步mysql数据变化到kafka(centos部署)
动态规划(树形dp)
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
Mlapi series - 04 - network variables and network serialization [network synchronization]
[detailed steps of FreeRTOS shift value for the first time]