当前位置:网站首页>LeetCode151-颠倒字符串中的单词-字符串-模拟
LeetCode151-颠倒字符串中的单词-字符串-模拟
2022-04-23 14:48:00 【李烦烦搞快点】
Note:
先翻转一下子,然后把每个单词抠出来,在翻转一下放到答案里面
好像也可以不开新的string 直接在原来的上面操作
class Solution {
public:
string reverseWords(string s) {
string ans;
reverse(s.begin(), s.end());
int p = 0;
while(p < s.size()){
string word = "";
while(s[p] == ' ' && p < s.size()) p ++;
while(s[p] != ' ' && p < s.size()) word += s[p ++];
reverse(word.begin(), word.end());
ans += word;
ans += ' ';
}
ans.pop_back();
if(ans[ans.size() - 1] == ' ') ans.pop_back();
return ans;
}
};
版权声明
本文为[李烦烦搞快点]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Mr_Ghost812/article/details/124350534
边栏推荐
- QT actual combat: Yunxi chat room
- 1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)
- Realization of four data flow modes of grpc based on Multilingual Communication
- Detailed explanation of C language P2 selection branch statement
- First acquaintance with STL
- 1-初识Go语言
- 8.4 循环神经网络从零实现
- Unity_代码方式添加绑定按钮点击事件
- Parameter stack pressing problem of C language in structure parameter transmission
- Don't you know the usage scenario of the responsibility chain model?
猜你喜欢
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
redis的五种数据类型
ArrayList collection basic usage
eolink 如何助力遠程辦公
Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring
Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
QT Detailed explanation of pro file
1n5408-asemi rectifier diode
Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
随机推荐
Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
Bingbing learning notes: take you step by step to realize the sequence table
博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
vscode中文插件不生效问题解决
Mds55-16-asemi rectifier module mds55-16
Brute force of DVWA low -- > High
Detailed explanation of C language P2 selection branch statement
基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
Arduino for esp8266串口功能简介
QT actual combat: Yunxi calendar
ASEMI三相整流桥和单相整流桥的详细对比
Outsourcing for four years, abandoned
PCIe X1 插槽的主要用途是什么?
select 同时接收普通数据 和 带外数据
冰冰学习笔记:一步一步带你实现顺序表
AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
Parameter stack pressing problem of C language in structure parameter transmission
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)