151. 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
".
Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.
click to show clarification.
- 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.
=====================
注意,
1,被空格包围的是单词
2,输入字符串可以以空格开头或结尾,但是结果中的字符不能以空格开头或结尾
3,输出字符串中单词间的空格是一个,不能重复出现空格.
思路:
对输入字符串进行去重空格操作,
对字符串中的每个单词进行反转
对整个字符串进行反转
====
code
class Solution {
public:
void help_reverse(string &s,int start,int end){
while(start<end){///经典的反转字符串方法
swap(s[start++],s[end--]);
}
}
string removeDuplicateSpace(string s){
string res;
int b = ;
for(;b<(int)s.size();b++){
if(s[b]!= ' '){
break;
}
}///
int e = s.size() - ;
for(;e>=;e--){
if(s[e]!=' '){
break;
}
} bool is_space = false;
for(int i = b;i<=e;i++){
if(s[i] == ' '){
if(!is_space) is_space = true;
else continue;
}else{
is_space = false;
}
res.push_back(s[i]);
}
return res;
}
void reverseWords(string &s) {
if(s.empty()) return;
s = removeDuplicateSpace(s);
int start = ;
for(size_t i = ;i<s.size();i++){
if(s[i]!=' '){
start = i;
}else{
continue;
}
size_t j = i;
while(j<s.size() && s[j]!=' '){
j++;
}
j--;
help_reverse(s,start,j);
i = j++;
}
help_reverse(s,,(int)s.size()-);
}
};
151. Reverse Words in a String的更多相关文章
- 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 ...
- 151. Reverse Words in a String(java 注意细节处理)
题目:reverse words in a string Given an input string, reverse the string word by word. For example,Giv ...
- [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
Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: ...
- (String)151. Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- Java for 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 --------- java
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 151. Reverse Words in a String翻转一句话中的单词
[抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...
- 151. Reverse Words in a String (String)
思路: 本题考查的目的并不是使用字符串的函数.方法是两次reverse,先对每个单词先做一次翻转,然后对整个字符串做一次翻转. 需要注意的是去除extra space,并且对全space字符串.以及最 ...
随机推荐
- JS的跨域问题
1.什么是跨域? 跨域问题是由于javascript语言安全限制中的同源策略造成的. 2.什么是同源策略: 同源策略是指一段脚本只能读取来自同一来源的窗口和文档的属性,这里的同一来源指的是主机名.协议 ...
- C#部分---函数添加基本格式;
格式1:没有参数,没有返回值 (无参无返) 添加函数: /// <summary> /// 累加求和的方法,没有参数,没有返回值 /// </summary> public v ...
- UVa 12100打印队列(队列)
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- leetcode 139. Word Break ----- java
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- python中字符串连接的三种方式
1.字符串之间连接 'aa' 'bb' 可以中间为空格 或者什么都没有. 那么输出都是两者之间紧密相连. 2.字符串+数字 'aa' +90 这样会报错,因为不同类型不能相加, 可以用 'aa',90 ...
- DNS-解析、劫持、污染
DNS( Domain Name System)是“域名系统”的英文缩写,是一种组织成域层次结构的计算机和网络服务命名系统,它用于TCP/IP网络,它所提供的服务是用来将主机名和域名转换为IP地址的工 ...
- docker安装错误
转载:http://www.roddypy.com/index.php/2016/03/11/centos7-yum-%E5%AE%89%E8%A3%85docker%E6%8A%A5%E9%94%9 ...
- PHP GC垃圾回收机制之引用变量回收周期疑问
普通的引用变量的销毁大家都知道, 当unset的时候如果refcount = 0 则认为无用, 销毁. 但是手册中提到一点会有递归引用的问题,很是奇葩 代码如下 <?php $a = 1; $a ...
- Web.Config文件中使用configSource
我们都知道,在asp.net中修改了配置文件web.config后,会导致应用程序重启,所有会话(session)丢失.然而,应用程序的配置信息放在配置文件里是最佳选择,在后台修改了配置后导致所有会话 ...
- $(function(){})和$(document).ready(function(){}) 的用法
当文档载入完毕就执行,以下几种效果是等价的:1. $(function(){ //这个就是jQuery ready()的简写,即下2的简写 // do something }); 2. $(docum ...