LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)
题目
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
- What constitutes a word?
A sequence of non-space characters constitutes a word. - Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces. - How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
分析
代码
#include <iostream>
#include <string>
#include <cwctype>
#include <vector>
using namespace std;
// 去除字符串首尾的空格
string& trim(string &s)
{
if(s.empty())
return s;
string::iterator iter;
for(iter = s.begin(); iter != s.end()&& iswspace(*iter++););
s.erase(s.begin(),--iter);
for(iter = s.end(); iter != s.begin()&& iswspace(*--iter););
s.erase(++iter,s.end());
return s;
}
// 翻转字符串中的某一段子串
void reverseString(string &s, int begin, int end)
{
for(; begin < end; begin++,end--)
{
char temp = s[begin];
s[begin] = s[end];
s[end] = temp;
}
}
// 删除单词之间多余的空格
void delete_mult_spaces(string &s)
{
string::iterator blank;
string::iterator iter = s.begin();
while(iter != s.end())
{
if(iswspace(*iter))
{
blank = iter + 1;
while(iswspace(*blank))
s.erase(blank);
iter = blank;
}
iter++;
}
}
void reverseWords(string &s)
{
if (s.empty())
return;
s = trim(s);
delete_mult_spaces(s);
reverseString(s, 0, s.length()-1);
int wbegin,wend;
wbegin = wend = 0;
for(size_t i = 0; i < s.length(); i++)
{
if(s[i] == ' ')
{
reverseString(s,wbegin,wend-1);
wbegin = wend + 1;
}
wend++;
// deal with the last word
if(i == s.length()-1)
reverseString(s,wbegin,wend-1);
}
}
int main()
{
vector<string> strs;
strs.push_back("the sky is blue");
strs.push_back(" the sky is blue");
strs.push_back("the sky is blue ");
strs.push_back("the sky is blue");
strs.push_back(" the sky is blue ");
strs.push_back("");
vector<string>::iterator iter;
for(iter = strs.begin(); iter != strs.end(); iter++)
{
reverseWords(*iter);
cout << *iter << endl;
}
return 0;
}
参考
LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)的更多相关文章
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...
- [LintCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- 151 Reverse Words in a String 翻转字符串里的单词
给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...
- Leetcode151. Reverse Words in a String翻转字符串里的单词
给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- 345. Reverse Vowels of a String翻转字符串中的元音字母
[抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
- 151. Reverse Words in a String翻转一句话中的单词
[抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...
随机推荐
- VS2015 创建C++动态库及使用
转载:https://blog.csdn.net/w_x_myself/article/details/82252646 1.dll的特点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代 ...
- 批量实现ssh免密登录
本节索引 场景分析 ssh免密登录 pssh工具批量管理 SHELL自动化脚本 本篇总结 场景分析 作为一个运维工程师,不是每个人工作的环境都想阿里.腾讯那样,动不动就上亿的PV量,上万台服务器.我们 ...
- 用GEOquery从GEO数据库下载数据--转载
https://www.plob.org/article/9969.html Gene Expression Omnibus database (GEO)是由NCBI负责维护的一个数据库,设计初衷是为 ...
- C# selenium 高级
https://www.cnblogs.com/morang/p/7441091.html https://www.cnblogs.com/tobecrazy/p/4817946.html https ...
- python 使用 elasticsearch 常用方法(聚合)
#记录聚合查询方法 from elasticsearch import Elasticsearch es = Elasticsearch(['xx.xx.xx.xx:9200']) #获取最小的年龄r ...
- redis-cli中文乱码
在开发过程中,需要验证redis缓存中的数据,发现redis存储的中文全是乱码,因为默认情况下redis不转义中文.如果在平常开发中想要看到中文内容,可以在使用redis-cli 命令登陆redis服 ...
- selenium===使用docker搭建selenium分布式测试环境
准备: #请在此之前先了解,selenium grid :参考:selenium-grid ,下载地址,win-本地部署过程 >>>环境准备: Linux操作系统 >>& ...
- EasyNVR网页摄像机无插件H5、谷歌Chrome直播方案之使用ffmpeg保存快照数据方法与代码
背景分析 EasyNVR主要功能模块有设备发现与接入.实时直播.摄像机控制.录像与管理.设备快照与状态维护.第三方平台对接,其中设备快照与状态维护主要包括定时检测通道设备的在线状态.定时对通道摄像机进 ...
- Centos各版本系统ISO镜像下载地址
https://www.centos.org/download/mirrors/ 需要在里面一个个看,有些是没有旧版本镜像的 补充: 上面这个方法很难再找到旧版本了 更好的方法如下:以下载Centos ...
- java 快速定位线上cpu偏高
1.top -c 加 大写P 查找高进程ID 2.top -Hp 加 大写 P 查找高线程ID 3.printf '%x\n' 线程ID 转成16进制 4.jstack 进程ID | grep 16进 ...