[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 ...
随机推荐
- Linux文件句柄数配置
1.单程序句柄数限制 查看配置的句柄数:ulimit -n cat /etc/security/limits.conf 参考配置: * soft nofile 655360* hard nofile ...
- Spring Cloud Eureka 使用外网IP和端口号进行服务注册
应用场景如下: 服务提供方(即要注册到服务中心的服务)的内网地址,外界无法访问(或者使用docker等做了应用端口等的配置),做了IP映射后,公网IP49.10.22.106映射到服务提供方的内网ip ...
- 跟我一起安装vmware
第一步查看我们的电脑配置 我是windows10,下面的方法是windows10来安装vmware 第二步双击下图文件 (1) 2)弹出如下图,点击下一步即可. (3)一直点击下一步(期间会同意,勾选 ...
- (补充)06.Hibernate持久化类&持久化对象
持久化类:就是一个Java类(JavaBean),这个类与表建立映射关系就可以成为是持久类 持久化类 = JavaBean + xxx.hbm.xml 编写规则: 1.提供一个无参数,public访问 ...
- 理解最短路径-Dijkstra算法
最短路径—Dijkstra算法和Floyd算法 透彻理解迪杰斯特拉算法 Dijkstra算法的使用条件:图中不存在负权边. ---------------------------有待验证------- ...
- redis学习笔记06-主从复制和哨兵机制
1.主从复制 为了保证线上业务的持续运行,防止主节点因宕机而重启数据恢复消耗太长时间,通常会准备一个备用节点,备份主节点的数据,当主节点出问题时立马顶上.这种机制就叫做主从复制.在了解redis的主从 ...
- 基于TensorFlow理解CNN中的padding参数
1 TensorFlow中用到padding的地方 在TensorFlow中用到padding的地方主要有tf.nn.conv2d(),tf.nn.max_pool(),tf.nn.avg_pool( ...
- 跟我一起做一个vue的小项目(APPvue2.5完结篇)
先放一下这个完结项目的整体效果 下面跟我我一起进行下面项目的进行吧~~~ 接下来我们进行的是实现header的渐隐渐显效果,并且点击返回要回到首页 我们先看效果 在处理详情页向下移动过程中,heade ...
- springboot之jpa(简述)
1.maven引入jar包(jpa和mysql) <dependency> <groupId>org.springframework.boot</groupId> ...
- 基于MaxCompute的媒体大数据开放平台建设
摘要:随着自媒体的发展,传统媒体面临着巨大的压力和挑战,新华智云运用大数据和人工智能技术,致力于为媒体行业赋能.通过媒体大数据开放平台,将媒体行业全网数据汇总起来,借助平台数据处理能力和算法能力,将有 ...