当前位置:网站首页>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; //一旦跳出循环返回字符串长度即为 两种情况下字符串的和。且注意顺序不可以改变 新的单词要在串的前面
}
边栏推荐
- IP课笔记(5)
- Accessing a one-dimensional array with a pointer
- Unity2d game let characters move - Part 1
- ue4 物品随机生成
- 简单却好用:使用Keras 2实现基于LSTM的多维时间序列预测
- Dameng database_ Common initialization parameters
- Synergy LAN realizes multi host shared keyboard and mouse (AMD, arm)
- 论文阅读-Endmember-Guided Unmixing Network (EGU-Net) 端元指导型高光谱解混网络
- Solve modularnotfounderror: no module named "cv2.aruco“
- 如何建立一个仪式感点满的网站,并发布到公网 1-2
猜你喜欢
随机推荐
MySQL基础---约束
IP作业(2)RIP
Lua Foundation
Hololens 2 development 101: create the first hololens 2 Application
IP课笔记(4)
Dameng database_ User password policy
IP课(OSPF)综合实验
Xshell remote access tool
记一次高校学生账户密码的获取,从无到有
异地远程连接在家里的群晖NAS【无公网IP,免费内网穿透】
Openpose2d转换3d姿态识别
Solve modularnotfounderror: no module named "cv2.aruco“
Foundation of JUC concurrent programming (1) -- related basic concepts
Channel attention and spatial attention module
Lunix命令入门 - 用户及文件权限(chmod 详解)
Unity(三)三维数学和坐标系统
unity2D游戏之让人物动起来-下
Paper reading endmember guided unmixing network (EGU net)
IP课笔记(5)
配置固定的远程桌面地址【内网穿透、无需公网IP】









