【刷题-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: "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: " hello world! "
Output: "world! hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: "a good example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
Note:
- A word is defined as a sequence of non-space characters.
- Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
- You need to reduce multiple spaces between two words to a single space in the reversed string.
解法1 分词,然后用字符串拼接
class Solution {
public:
string reverseWords(string s) {
string tmp, res;
for(int i = 0; i < s.size(); ++i){
if(s[i] == ' '){
if(tmp.size() > 0){
res = tmp + " " + res;
tmp = "";
}
}else{
tmp += s[i];
}
}
res = tmp + " " + res;
int i = 0, j = res.size() - 1;
while(i < s.size() && res[i] == ' ')i++;
while(j >= 0 && res[j] == ' ')j--;
return res.substr(i, j-i+1);
}
};
解法2 字符串翻转。先将字符串整体翻转,然后将每个单词翻转。注意需要预处理除掉字符串首尾的空格,翻转结束后需要去除字符串中间多余的空格
class Solution {
public:
string reverseWords(string s) {
while(s.size() > 0 && s[0] == ' ')s.erase(0,1);
while(s.size() > 0 && s[s.size()-1] == ' ')s.erase(s.size()-1, 1);
reverse(s, 0, s.size()-1);
int pre = 0, cur = 0;
while(cur < s.size()){
if(s[cur] == ' '){
reverse(s, pre, cur-1);
cur += 1;
pre = cur;
}else{
cur += 1;
}
}
reverse(s, pre, cur-1);
int i = 1;
while(i < s.size()){
if(s[i] == ' ' && s[i-1] == ' ')s.erase(i,1);
else i++;
}
return s;
}
void reverse(string &s, int pre, int cur){
while(pre < cur){
char tmp = s[pre];
s[pre] = s[cur];
s[cur] = tmp;
pre++;
cur--;
}
}
};
【刷题-LeetCode】151 Reverse Words in a String的更多相关文章
- [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刷题笔记】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 word 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& ...
- Leetcode#151 Reverse Words in a String
原题地址 将单词按空格分词,然后倒序拼接即可 代码: void reverseWords(string &s) { vector<string> words; ; ; ; i &l ...
- [leetcode]151. Reverse Words in a String翻转给定字符串中的单词
Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...
- [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- 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 ...
随机推荐
- LuoguP6850 NOI 题解
Content 小 L 参加了 \(\texttt{NOI}\),现在他告诉你九个数 \(a,b,c,d,e,f,g,h,i\),分别表示--笔试作对的题数.D1T1.D1T2.D1T3.D2T1.D ...
- Java的垃圾回收机制:强制回收System.gc() Runtime.getTime().gc()
垃圾回收 当引用类型的实体,如对象.数组等不再被任何变量引用的时候.这块占用的内存就成为了垃圾.JVM会根据自己的策略决定是回收内存 注意: 垃圾回收只回收内存中的对象,无法回收物理资源(数据库连接, ...
- 存储技术之ceph了解
ceph rados:可靠的.自动的.分布式.对象存储 特性:高效性,统一性(文件存储,块存储,对象存储),可扩展 没有数据库的概念:为cluster map 记录集群状态. PG:(ceph核心单位 ...
- netty系列之:小白福利!手把手教你做一个简单的代理服务器
目录 简介 代理和反向代理 netty实现代理的原理 实战 总结 简介 爱因斯坦说过:所有的伟大,都产生于简单的细节中.netty为我们提供了如此强大的eventloop.channel通过对这些简单 ...
- AcWing1264. 动态求连续区间和 (树状数组做法)
1.题目 给定 n 个数组成的一个数列,规定有两种操作,一是修改某个元素,二是求子数列 [a,b] 的连续和. 输入格式 第一行包含两个整数 n 和 m,分别表示数的个数和操作次数. 第二行包含 n ...
- ACwing1015. 摘花生
题目: Hello Kitty想摘点花生送给她喜欢的米老鼠. 她来到一片有网格状道路的矩形花生地(如下图),从西北角进去,东南角出来. 地里每个道路的交叉点上都有种着一株花生苗,上面有若干颗花生,经过 ...
- Linux(centos7)安装ClickHouse
Clickhouse 仅支持Linux 且必须支持SSE4.2 指令集 grep -q sse4_2 /proc/cpuinfo && echo "SSE 4.2 suppo ...
- c++设计模式概述之代理
代码写的不规范,目的是缩短文章篇幅,实际中请不要这样做. 1.模式的结构 代理模式的主要角色如下: A.抽象主题(Subject)类:通过接口或抽象类声明真实主题和代理对象实现的业务方法. B.真实主 ...
- 【LeetCode】1041. Robot Bounded In Circle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 日期 题目地址:https://leetco ...
- 移动端H5-iPhone安全距离适配
安全区域? 安全区域指的是一个可视窗口范围,处于安全区域的内容不受圆角(corners).齐刘海(sensor housing).小黑条(Home Indicator)影响,如下图蓝色区域: 也就是说 ...