当前位置:网站首页>leetcode剑指offer JZ73 翻转单词序列
leetcode剑指offer JZ73 翻转单词序列
2022-07-24 05:22:00 【喜乐自在】

这道题主要是考察对string类对象字符串理解与使用:
代码注释如下
#include<iostream>
using namespace std;
class Solution {
public:
string ReverseSentence(string str) {
if (str.empty()) return str; //特殊情况的话直接返回空值
string ret = ""; //设置两个空字符串
string tmp = "";
for (int i = str.size()- 1; i >= 0; i--) { //由于字符串倒置,故从尾部开始往前遍历
// 合并一个单词
if (str[i] != ' ') {
tmp = str[i]+tmp; //注意字符串相加时的顺序 新的字符要在tmp字符前
}
// 找到一个单词,将单词合并到结果串中
else if (str[i] == ' ' ) {
ret = ret+tmp + " "; //同上 新的单词要在 原单词前面,顺序不可以改变
tmp = "";
}
}
return ret+tmp; //一旦跳出循环返回字符串长度即为 两种情况下字符串的和。且注意顺序不可以改变 新的单词要在串的前面
}
边栏推荐
猜你喜欢

UE4 aiming offset

使用Keras实现 基于注意力机制(Attention)的 LSTM 时间序列预测

unity2D游戏之让人物动起来-下

Positional argument after keyword argument

Day3 jvm+ sorting summary

JDBC初级学习 ------(师承尚硅谷)

C language linked list (create, traverse, release, find, delete, insert a node, sort, reverse order)

配置固定的远程桌面地址【内网穿透、无需公网IP】

Dameng database_ Logical architecture foundation

如何建立一个仪式感点满的网站,并发布到公网 2-2
随机推荐
Data warehouse and data warehouse modeling
unity2D横版游戏跳跃实时响应
Write the list to txt and directly remove the comma in the middle
Day3 jvm+ sorting summary
Day2 websocket+ sort
Dameng database_ Logical backup
IP作业(6)
JDBC advanced -- learning from Shang Silicon Valley (DAO)
MySQL download and installation environment settings
HoloLens 2 开发101:创建首个HoloLens 2应用程序
IP笔记(10)
Simple but easy to use: using keras 2 to realize multi-dimensional time series prediction based on LSTM
MySQL基础---约束
day4-jvm
简单三步快速实现内网穿透
Dameng database_ Common user management commands
Unity(三)三维数学和坐标系统
Foundation of JUC concurrent programming (8) -- read write lock
什么是单调队列
Basic knowledge of unity and the use of some basic APIs