[leetcode] Reverse Words in a String [1]
一、题目:
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.
二、解题思想
翻转字符串中的单词顺序,这是个老题目了。可是leetcode上面的要求更为严格。如:
要求把开头和结尾的空格删除掉;
缩减单词间的空格数为1(假设有多个空格)。
单词若全是空格,则返回一个空字符串("").
此题思想不难。主要是注意上面三个要求和一些细节就能够AC。
大致分为两步:一个是常规的翻转字符串中的单词;还有一个就是想方法去掉串中的多余的单词;这两步骤的顺序能够颠倒。
以下给出两份代码。第一个代码是先去掉多余的空格。然后在翻转;第二个代码先翻转,在去掉多余的空格。就效率上来说应该是第一个代码的效率更高。
三、代码实现
代码一:
class Solution {
public:
void reverseWords(string &s) {
if(s.size()<=0) return ;
char *work = new char[s.size()+1];
//reduce blank
int j=0;
for(int i=0; s[i]!='\0'; ++i){
if(i>0 && s[i] == ' ' && s[i-1]!= ' ')
work[j++] = s[i];
else if(s[i] != ' ')
work[j++] = s[i];
}
if(j>0 && work[j-1]==' ')
work[--j] = '\0';
else
work[j] = '\0';
//reverse all string
reverse(work, 0, j-1);
int p= 0, i=0;
//reverse each word
while(i<j){
while(p<j && work[p]!=' ') p++;
reverse(work, i, p-1);
i = p+1;
p = i;
}
string temp(work);
s = temp;
}
void reverse(char *s, int beg, int end){
while(beg < end){
char temp = s[beg];
s[beg++] = s[end];
s[end--] = temp;
}
}
};
代码二:
class Solution {
public:
void reverseWords(string &s) {
int n = s.size();
if(n<=0) return;
//if(n==1)
//reverse the whole string
reverse(s, 0, n-1);
//reverse each word
int begin=0, end = 0;
while(begin<n){
while( begin< n && s[begin] == ' ') ++begin;
end = begin;
while( end<n && s[end] != ' ') ++end;
reverse(s, begin, end-1);
begin = end;
}
//reduce blank
begin = 0;
while(begin<n && s[begin] ==' ') ++begin;
if(begin == n) {s = s.substr(0,0);return;}
end = n-1;
while(end>=0 && s[end] == ' ') --end;
int start = 0;
char pre;
for(; begin<=end; ++begin){
if(s[begin] != ' '){
s[start++] = s[begin];
pre = s[begin];
}else{
if(pre != ' '){
s[start++] = ' ';
pre = ' ';
}
}
}
if(start != n) s = s.substr(0, start);
}
void reverse(string &s, int begin, int end){
char temp;
while(begin<end){
temp = s[begin];
s[begin++] = s[end];
s[end--] = temp;
}
}
};
另外,我开通了微信公众号--分享技术之美。我会不定期的分享一些我学习的东西.
)
[leetcode] Reverse Words in a String [1]的更多相关文章
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- [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 ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [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 Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- LeetCode: Reverse Words in a String 解题报告
Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...
- LeetCode Reverse Vowels of a String
原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...
- LeetCode: Reverse Words in a String && Rotate Array
Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...
- [leetcode]Reverse Words in a String @ Python
原题地址:https://oj.leetcode.com/problems/reverse-words-in-a-string/ 题意: Given an input string, reverse ...
随机推荐
- Unity优化垃圾回收GC
- Activiti流程定义语言
1.流程(process) bpmn文件一个流程的根元素.一个流程就代表一个工作流. 2.顺序流(sequenceFlow) 顺序流是连接两个流程节点的连线,代表一个节点的出口.流程执行完一个节点后, ...
- AlexNet模型
AlexNet模型 <ImageNet Classification with Deep Convolutional Neural Networks>阅读笔记 一直在使用AlexNet,本 ...
- 使用 top instance 命令查看运行中 MaxCompute 作业
我们都知道,在 MaxCompute Console 里,可以使用下面的命令来列出运行完成的 instance 列表. show p|proc|processlist [from <yyyy-M ...
- 3926: [Zjoi2015]诸神眷顾的幻想乡
传送门 一个广义后缀自动机模板. //Achen #include<algorithm> #include<iostream> #include<cstring> ...
- KOA 学习(六)superAgent
原文地址 http://www.2cto.com/kf/201611/569080.html 基本请求 初始化一个请求可以通过调用request模块中适当的方法,然后使用.end()来发送请求,例如一 ...
- eclipse设置提示信息
1.设置 java 文件的代码提示功能 .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ () 2.设置 xml 文件的代码提示功能 打 开 ...
- vim编辑器操作①
Linux文本编辑器: 行编辑器:sed 全屏编辑器:nano,vi/vim 本文主要介绍说明vim编辑器的相关使用: 其有三种模式,即: 编辑模式(默认模式).插入模式(输入模式).末行模式(内置的 ...
- git pull拉取远程分支时出现冲突
现象:在git clone一个项目后,默认是master分支,但是如果想要切换到另一个已经存在的dev分支,那么不要先在本地创建dev分支再拉取远程的dev分支,而是应该直接切换到dev分支,然后再拉 ...
- Django项目:CRM(客户关系管理系统)--33--25PerfectCRM实现King_admin添加出错修复
{#table_change.html#} {## ————————19PerfectCRM实现King_admin数据修改————————#} {#{% extends "king_mas ...