【刷题-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 ...
随机推荐
- CF450B Jzzhu and Sequences 题解
Content 有一个长度为 \(n\) 的数列 \(\{a_1,a_2,\dots,a_n\}\),满足如下的递推公式: \(i=1\) 时,\(a_1=x\). \(i=2\) 时,\(a_2=y ...
- SSM——Spring框架
Spring概念 什么是Spring Spring两大核心 1.IOC 所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管理过程交由Spring框架来处理,从此在开发过程中 ...
- float浮动的详细总结
float浮动的详细总结 1.定位方案 在css中,有4种常用的方法对元素进行定位和布局: normal flow:标准流.文档流: position:定位(relative.absolute.fix ...
- 【LeetCode】752. Open the Lock 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Java学到什么程度可以面试工作?
先说结论: 1 大多数公司,对于Java初级开发的要求是,会用Spring Boot+JPA做增删改查 2 所以零基础的Java小白,无需学太多的内容,只要掌握Spring Boot+JPA做增删改 ...
- 【OpenXml】Pptx的边框虚线转为WPF的边框虚线
安装Openxml sdk 首先,我们先安装nuget的需要的有关的Openxml sdk,我们开源了解析pptx的Openxml拍平层,下面两种方式都可以安装: nuget包管理器控制台: Inst ...
- 写了个适用于vscode的minio图床客户端插件
缘起 自己搭建minio做我的个人博客图床好一段时间了, 一直用minio自带的web管理后台来上传图片, 它的界面长下面这个样子 上传完后, 需要点下文件列表里刚刚传上去的文件的分享按钮 然后会出来 ...
- 知识增强的预训练语言模型系列之ERNIE:如何为预训练语言模型注入知识
NLP论文解读 |杨健 论文标题: ERNIE:Enhanced Language Representation with Informative Entities 收录会议:ACL 论文链接: ht ...
- 如何在Eclipse中搭建MyBatis基本开发环境?(使用Eclipse创建Maven项目)
实现要求: 在Eclipse中搭建MyBatis基本开发环境. 实现步骤: 1.使用Eclipse创建Maven项目.File >> New >> Maven Project ...
- 编写Java程序,随机给定一个数字猜大小
返回本章节 返回作业目录 需求说明: 由系统随机生成一个1~100之间的整数. 通过控制台一直输入一个整数,比较该数与系统随机生成的那个数,如果大就输出"猜大了.",继续输入:如果 ...