当前位置:网站首页>力扣(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--;
}
}
};
边栏推荐
- Tengine kernel parameters
- Lombok principle and the pit of ⽤ @data and @builder at the same time
- SharedPreferences 源码分析
- 解决“C2001:常量中有换行符“编译问题
- E. Best Pair
- HotSpot VM
- Selection sort
- Crawler notes: improve data collection efficiency! Use of proxy pool and thread pool
- 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
- Ue5 small knowledge freezerendering view rendered objects in the cone
猜你喜欢
Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
Certbot failed to update certificate solution
Distributed transaction solution
Delete subsequence < daily question >
Embedded development program framework
Solution of storage bar code management system in food industry
[FreeRTOS interrupt experiment]
Vulnerability discovery - vulnerability probe type utilization and repair of web applications
Overturn your cognition? The nature of get and post requests
coreldraw2022新版本新功能介绍cdr2022
随机推荐
Dry goods collection | Vulkan game engine video tutorial
One question per day (Mathematics)
I'd like to ask about the current MySQL CDC design. In the full volume phase, if a chunk's binlog backfill phase,
CADD course learning (7) -- Simulation of target and small molecule interaction (flexible docking autodock)
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
VPP performance test
The most detailed and comprehensive update content and all functions of guitar pro 8.0
View workflow
hashlimit速率控制
Mlapi series - 04 - network variables and network serialization [network synchronization]
newton interpolation
POI add border
[tomato assistant installation]
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
Dynamic programming (tree DP)
npm命令--安装依赖包--用法/详解
[HBZ sharing] how to locate slow queries in cloud database
How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
Scala function advanced