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& ...
随机推荐
- [转]C/C++实现回调机制的几种方式(回调、槽、代理)
转自:https://www.jianshu.com/p/4f907bba6d5f (1)Callback方式(回调) Callback的本质是设置一个函数指针进去,然后在需要需要触发某个事件时调用该 ...
- [原]globalmapper设置高程配色(globalmapper自定义配色方案)
1.使用的globalmapper版本:1.8以上(之前的版本也应该支持) 2.将全球DEM加载进去 (零时找的小DEM 全球7级) 3.右击此处,选择“高程图例选项” 4.选择 配置-着色器选项 ...
- iobit-unlocker --- 类似 Unlocker 工具,强制删除文件或文件夹
win10 使用 Unlocker 1.9.2 常有问题,以前在win7上使用完全ok的 更换成:iobit-unlocker ,win10体验还可以,类似Unlocker 下载地址: https:/ ...
- Python3基础 __file__ 查询模块的完整路径
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- 安装opencv时ippicv下载超时
1.手动去下载: github地址为: https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20151201/ippicv 2.查 ...
- LeetCode 100. Same Tree (判断树是否完全相同)
100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...
- spring 支持集中 bean scope?
Spring bean 支持 5 种 scope: Singleton - 每个 Spring IoC 容器仅有一个单实例. Prototype - 每次请求都会产生一个新的实例. Request - ...
- Failed to open .vcf.gz: could not load index
这类报错在我使用bcftools index file.vcf.gz进行index出现的. 解决办法是换用tabix进行index,命令为tabix -p vcf file.vcf.gz. 用tabi ...
- 安卓 App 性能专项测试之流畅度深度解析-上篇
指标背景 流畅度,顾名思义是用户感知使用App页面时的流畅情况,"App卡不卡",这是用户最直接的感受. 但是要用量化之后的数据衡量流畅度,在Android平台这边并没有直接有效的 ...
- noi openjudge 1768:最大子矩阵
链接:http://noi.openjudge.cn/ch0406/1768/ 描述已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如 ...