当前位置:网站首页>Leetcode 186 Flip the word II in the string (2022.07.05)
Leetcode 186 Flip the word II in the string (2022.07.05)
2022-07-06 04:43:00 【ChaoYue_ miku】
Given a string , Flip each word in the string one by one .
Example :
Input : [“t”,“h”,“e”," “,“s”,“k”,“y”,” “,“i”,“s”,” “,“b”,“l”,“u”,“e”]
Output : [“b”,“l”,“u”,“e”,” “,“i”,“s”,” “,“s”,“k”,“y”,” ",“t”,“h”,“e”]
Be careful :
A word is defined as a series of characters that do not contain spaces
The input string will not contain leading or trailing spaces
Words are always separated from each other by a single space
Advanced : Use O(1) In situ solution of extra space complexity .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/reverse-words-in-a-string-ii
Method 1 : Double pointer
C++ Submission :
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--;
}
}
};
边栏推荐
- Ue5 small knowledge freezerendering view rendered objects in the cone
- Easyrecovery reliable and toll free data recovery computer software
- Lagrange polynomial
- 11. Intranet penetration and automatic refresh
- canal同步mysql数据变化到kafka(centos部署)
- The most detailed and comprehensive update content and all functions of guitar pro 8.0
- [HBZ sharing] how to locate slow queries in cloud database
- Visio draws Tai Chi
- I'd like to ask about the current MySQL CDC design. In the full volume phase, if a chunk's binlog backfill phase,
- [Chongqing Guangdong education] Suzhou University English film and Television Appreciation reference materials
猜你喜欢
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
How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
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
Coreldraw2022 new version new function introduction cdr2022
11. Intranet penetration and automatic refresh
捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统
Delete subsequence < daily question >
IPv6 comprehensive experiment
Case of Jiecode empowerment: professional training, technical support, and multiple measures to promote graduates to build smart campus completion system
SQL injection vulnerability (MSSQL injection)
随机推荐
ORM aggregate query and native database operation
Redis - redis in action - redis actual combat - actual combat Chapter 1 - SMS login function based on redis - redis + token shared session application - with code
What should the project manager do if there is something wrong with team collaboration?
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)
Sentinel sliding window traffic statistics
Case of Jiecode empowerment: professional training, technical support, and multiple measures to promote graduates to build smart campus completion system
Yyds dry goods inventory OSI & tcp/ip
MIT CMS. 300 session 8 – immersion / immersion
比尔·盖茨晒18岁个人简历,48年前期望年薪1.2万美元
Programmers' position in the Internet industry | daily anecdotes
几种RS485隔离通讯的方案介绍
Luogu deep foundation part 1 Introduction to language Chapter 2 sequential structure programming
关于imx8mp的es8316的芯片调试
【HBZ分享】ArrayList的增删慢查询快的原因
8. Static file
Introduction to hashtable
2328. Number of incremental paths in the grid graph (memory search)
SQL注入漏洞(MSSQL注入)
MPLS experiment
C'est un petit résumé de l'étude.