Reverse Words in a String
Problem:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
这个题目叙述的不够精确,有些边界的情况需要考虑。题目的下方给出了Clarification澄清了几个问题,也是面试的时候需要确认的。
第一个问题是什么构成了一个词(word),回答是一串不包含空格(non-space)的字符。这里需要理解non-space,应该要包含' ', '\t', '\n'等字符,C语言中可以通过宏定义isspace来判断。
第二个问题是词的开头与结尾能有空格吗,回答是有的,但返回的结果中应该去掉。
第三个问题是分隔每两个词之间的空格能有多个吗,回答是能,但返回结果中应该只留下一个空格就好。
通过询问这三个问题,题目也就清楚多了,这个题目的解决思路并不困难,就是先翻转每个单词,再对整个字符串翻转就能做到翻转每个单词的目的,但是同时要想将单词间的空格变为一个,头尾都要去掉就要费一番功夫。
我的解法是直接在原始的字符串上一遍扫描,从后向前翻转单词,同时将单词移位到正确的位置,即每两个单词之间保证只有一个空格。然后整体再翻转一次,只有结尾处还可能有些空格,再单独处理。代码中使用了algorithm中的reverse来翻转字符串,效率可能会稍差,可以自己直接用下标操作,写个翻转函数。
1 void reverseWords(string &s) { 2 int len = s.length(); 3 4 // find each word iteratively and then reserve it 5 int align = len-1; 6 int start_pos, end_pos; 7 for(int i = len-1; i >= 0; --i) { 8 if(!isspace(s[i])) { 9 end_pos = i; 10 11 while(i >= 0 && !isspace(s[i])) { 12 --i; 13 } 14 start_pos = i+1; 15 16 reverse(s.begin()+start_pos, s.begin()+end_pos+1); 17 18 if(end_pos != align) { 19 for(int j = 0; j <= end_pos-start_pos; ++j) { 20 s[align-j] = s[end_pos-j]; 21 } 22 } 23 align -= (end_pos-start_pos+1); 24 if(align >= 0) { 25 s[align--]= ' '; 26 } 27 } 28 } 29 30 if(align >= 0) { 31 fill(s.begin(), s.begin()+align+1, ' '); 32 } 33 34 // reverse the whole string and trim the leading and trailing spaces 35 reverse(s.begin(), s.end()); 36 37 int i = len-1; 38 while(i >= 0 && isspace(s[i])) 39 --i; 40 s = s.substr(0, i+1); 41 }
转载于:https://www.cnblogs.com/foreverinside/p/3694630.html
Reverse Words in a String相关推荐
- leetcode day1 -- Reverse Words in a String Evaluate Reverse Polish Notation Max Points on a Li
以前从来没做过什么oj,发现做oj和在本地写代码或者纸上写差别还是很大的,觉得今天开始刷oj,特此记录一下. 1.Reverse Words in a String Given an input st ...
- Reverse Vowels of a String (反转字符串中的母音)
leetcode Reverse Vowels of a String 反转字符串中的母音 一.学习要点: 1.find_first_of:查找与字符串str中某个字符相同的位置,并返回他的第一个出现 ...
- leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public:string reverseWords(string ...
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- LeetCode 151. Reverse Words in a String
151. Reverse Words in a String Given an input string, reverse the string word by word. For example, ...
- LeetCode345. Reverse Vowels of a String
345. Reverse Vowels of a String My Submissions QuestionEditorial Solution Total Accepted: 3821 Total ...
- 345.反转字符串中的元音字母(Reverse Vowels of a String)
题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 给定 s = "hello", 返回 "holle". 示例 2: 给定 s ...
- 从零开始的LC刷题(74): Reverse Vowels of a String
原题: Write a function that takes a string as input and reverse only the vowels of a string. Example 1 ...
- c++内置函数实现字符串翻转(reverse,strrev,string 构造函数)
c++内置函数实现字符串翻转(reverse,strrev,string 构造函数) 在写程序的时候,我们经常需要将字符串进行翻转.c++中内置的函数有不少个可以实现该功能. 1.strrev函数.( ...
- leetcode 151 Reverse Words in a String (python)
leetcode 151 Reverse Words in a String 题目描述: Given an input string, reverse the string word by wor ...
最新文章
- 无穷级数求和7个公式_双色球2019129期渗透围红蓝(6+1实战,附:7个双色球胆码公式)...
- java试用(1)hello world
- 数据科学之基石:数据科学家必须掌握的10个统计学概念
- mysql不用limit怎么分页_mysql limit 分页的用法及注意要点
- Lua === Lua 十分钟基础入门上手
- Linux下的 FTP
- python中dic_python之dic {字典}(重要指数*****)
- java数组有顺序吗_java – 使用特定顺序对(数组)列表进行排序
- easyui input输入框的限制和校验条件
- MySQL高级-存储过程和函数
- 实现putty基于密钥的安全登录
- 请客必备:【转】100元以内南宁市有特色的餐馆
- Java获取电脑外网ip地址方法
- 服务器端获取数据(一)
- 2018面经(1):机器视觉工程师岗位
- 什么是两化融合和数字化转型?
- 《初级会计实务》考试学习分享之第八章 ——政府会计基础【考试大纲】
- 3, excel vba 获取表格里的单元格的值
- 数据库常用函数的使用案例
- 目前市面上的固码有哪些品牌
热门文章
- 基于 Docker 和 GitLab 的前端自动化部署实践笔记
- Pyserial 实现串口 base on python3
- 通过Ajax来简单的实现局部刷新(主要为C#中使用的UpdatePanel控件和ScriptManager控件)...
- Rabbit MQ 学习笔记(3)角色列表
- solr创建core
- php-echo原理
- html5开发windows8应用 windows8介绍
- 《Effective C#》某些地方实在是对不起Effective这个词(I)
- 压缩包解压后SecureCRT无法连接的解答
- 自己手写一个Mybatis框架(简化)