当前位置:网站首页>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--;
}
}
};
边栏推荐
- L'introduction en bourse de MSK Electronics a pris fin: 800 millions de RMB d'actifs de Henan étaient des actionnaires
- 1008 circular right shift of array elements (20 points)
- How to estimate the population with samples? (mean, variance, standard deviation)
- Postman断言
- The kernel determines whether peripherals are attached to the I2C address
- Quatre méthodes de redis pour dépanner les grandes clés sont nécessaires pour optimiser
- Introduction of several RS485 isolated communication schemes
- 【Try to Hack】john哈希破解工具
- What should the project manager do if there is something wrong with team collaboration?
- Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
猜你喜欢

The value of two date types is subtracted and converted to seconds

Mysql database storage engine

RTP GB28181 文件测试工具

SQL injection vulnerability (MSSQL injection)

The implementation of the maize negotiable digital warehouse receipt standard will speed up the asset digitization process of the industry

Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)

Recommendation | recommendation of 9 psychotherapy books
![[05-1, 05-02, 05-03] network protocol](/img/25/2e9ccc3f31a1fd46c9ab643d48064b.jpg)
[05-1, 05-02, 05-03] network protocol

比尔·盖茨晒18岁个人简历,48年前期望年薪1.2万美元

Fedora/REHL 安装 semanage
随机推荐
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
Dry goods collection | Vulkan game engine video tutorial
How does computer nail adjust sound
The value of two date types is subtracted and converted to seconds
关于imx8mp的es8316的芯片调试
也算是學習中的小總結
Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
[face recognition series] | realize automatic makeup
Bubble sort
Postman前置脚本-全局变量和环境变量
Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
[leetcode question brushing day 33] 1189 The maximum number of "balloons", 201. The number range is bitwise AND
Redis 排查大 key 的4種方法,優化必備
Sentinel sliding window traffic statistics
. Net interprocess communication
Platformio create libopencm3 + FreeRTOS project
Uva1592 Database
What should the project manager do if there is something wrong with team collaboration?
Selection sort