当前位置:网站首页>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--;
}
}
};
边栏推荐
- Programmers' position in the Internet industry | daily anecdotes
- Easyrecovery靠谱不收费的数据恢复电脑软件
- View 工作流程
- [mathematical modeling] differential equation -- sustainable development of fishing industry
- How does vs change the project type?
- SQL injection vulnerability (MSSQL injection)
- 11. Intranet penetration and automatic refresh
- ue5 小知识 FreezeRendering 查看视锥内渲染的物体
- [Chongqing Guangdong education] Suzhou University English film and Television Appreciation reference materials
- Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
猜你喜欢
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
View workflow
Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
The value of two date types is subtracted and converted to seconds
[face recognition series] | realize automatic makeup
Redis —— Redis In Action —— Redis 实战—— 实战篇一 —— 基于 Redis 的短信登录功能 —— Redis + Token 的共享 session 应用— 有代码
How to realize automatic playback of H5 video
ue5 小知识点 开启lumen的设置
Delete subsequence < daily question >
[network] channel attention network and spatial attention network
随机推荐
[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University
Etcd database source code analysis -- etcdserver bootstrap initialization storage
Fedora/REHL 安装 semanage
cdc 能全量拉去oracle 表嘛
Complete list of common functions of turtle module
麦斯克电子IPO被终止:曾拟募资8亿 河南资产是股东
How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
Redis 排查大 key 的4種方法,優化必備
CADD course learning (8) -- virtual screening of Compound Library
[HBZ sharing] how to locate slow queries in cloud database
Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill
内核判断i2c地址上是否挂载外设
也算是学习中的小总结
L'introduction en bourse de MSK Electronics a pris fin: 800 millions de RMB d'actifs de Henan étaient des actionnaires
Digital children < daily question> (Digital DP)
Distributed transaction solution
Redis - redis in action - redis actual combat - actual combat Chapter 1 - SMS login function based on redis - redis + token shared session application - with code
Project manager, can you draw prototypes? Does the project manager need to do product design?
C'est un petit résumé de l'étude.
MIT CMS. 300 session 8 – immersion / immersion