【刷题-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 ...
随机推荐
- CF808B Average Sleep Time 题解
Content 给定 \(n\) 个数 \(a_1,a_2,a_3,...,a_n\),求所有长度为 \(k\) 的连续区间 \([a_1,a_k],[a_2,a_{k+1}],...[a_{n-k+ ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【LeetCode】802. Find Eventual Safe States 解题报告(Python)
[LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- 【LeetCode】853. Car Fleet 解题报告(Python)
[LeetCode]853. Car Fleet 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...
- B. Petya and Exam
B. Petya and Exam 题目链接 题意 给你一串字符,在这个串中所有出现的字符都是\(good\)字符,未出现的都是\(bad\)字符, 然后给你另一串字符,这个字符串中有两个特殊的字符, ...
- Discrete Logging(poj2417)
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5120 Accepted: 2319 ...
- Codeforce 633C. Spy Syndrome 2
C. Spy Syndrome 2 time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- How many integers can you find(hdu1796)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Mysql溯源-任意文件读取👻
Mysql溯源-任意文件读取 前言 读了<MySQL蜜罐获取攻击者微信ID>的文章,文中说明了通过mysql蜜罐读取攻击者微信ID的过程,抱着学习的态度尝试了一下 原理 mysql中有一个 ...
- JavaWeb项目作业 Market商品管理系统
目录 一.语言和环境 二.实现功能 三.数据库设计 四.实现代码 一.语言和环境 实现语言:Java语言. 环境要求:MyEclipse(Eclipse)+MySQL. 实现方式:JBDC.jsp/s ...